<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"><title>开源应用容器引擎Docker_03常用组件Docker安装 | Oct25-X</title><meta name="keywords" content="Docker"><meta name="author" content="Oct25-X"><meta name="copyright" content="Oct25-X"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="Docer 安装整体步骤 搜索镜像 拉取镜像 查看镜像 启动镜像 服务端口映射   停止容器 移除容器  单机部署 MySQL&#x2F;Redis一、安装 MySQL8安装步骤 拉取镜像  1docker pull mysql:8.0   查看镜像  12345678[root@hadoop100 ~]$ docker imagesREPOSITORY              TAG">
<meta property="og:type" content="article">
<meta property="og:title" content="开源应用容器引擎Docker_03常用组件Docker安装">
<meta property="og:url" content="https://oct25-x.atomgit.net/oct25-xxxxx/2023/11/22/37%E5%BC%80%E6%BA%90%E5%BA%94%E7%94%A8%E5%AE%B9%E5%99%A8%E5%BC%95%E6%93%8EDocker_03%E5%B8%B8%E7%94%A8%E7%BB%84%E4%BB%B6Docker%E5%AE%89%E8%A3%85/index.html">
<meta property="og:site_name" content="Oct25-X">
<meta property="og:description" content="Docer 安装整体步骤 搜索镜像 拉取镜像 查看镜像 启动镜像 服务端口映射   停止容器 移除容器  单机部署 MySQL&#x2F;Redis一、安装 MySQL8安装步骤 拉取镜像  1docker pull mysql:8.0   查看镜像  12345678[root@hadoop100 ~]$ docker imagesREPOSITORY              TAG">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://oct25-x.atomgit.net/oct25-xxxxx/img/cover_img/ctt37.png">
<meta property="article:published_time" content="2023-11-22T01:50:13.000Z">
<meta property="article:modified_time" content="2024-02-17T14:32:50.588Z">
<meta property="article:author" content="Oct25-X">
<meta property="article:tag" content="Docker">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://oct25-x.atomgit.net/oct25-xxxxx/img/cover_img/ctt37.png"><link rel="shortcut icon" href="/oct25-xxxxx/img/logo.jpg"><link rel="canonical" href="https://oct25-x.atomgit.net/oct25-xxxxx/2023/11/22/37%E5%BC%80%E6%BA%90%E5%BA%94%E7%94%A8%E5%AE%B9%E5%99%A8%E5%BC%95%E6%93%8EDocker_03%E5%B8%B8%E7%94%A8%E7%BB%84%E4%BB%B6Docker%E5%AE%89%E8%A3%85/"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/oct25-xxxxx/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@6/css/all.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/oct25-xxxxx/',
  algolia: undefined,
  localSearch: {"path":"search.json","languages":{"hits_empty":"找不到您查询的内容：${query}"}},
  translate: {"defaultEncoding":2,"translateDelay":0,"msgToTraditionalChinese":"繁","msgToSimplifiedChinese":"簡"},
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: undefined,
  source: {
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery@2/dist/fjGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery@2/dist/fjGallery.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isAnchor: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: '开源应用容器引擎Docker_03常用组件Docker安装',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2024-02-17 22:32:50'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
    const detectApple = () => {
      if(/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    })(window)</script><link rel="stylesheet" href="/css/rightMenu.css"><meta name="generator" content="Hexo 6.1.0"></head><body><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="/oct25-xxxxx/img/avatar.jpg" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="site-data is-center"><div class="data-item"><a href="/oct25-xxxxx/archives/"><div class="headline">文章</div><div class="length-num">52</div></a></div><div class="data-item"><a href="/oct25-xxxxx/tags/"><div class="headline">标签</div><div class="length-num">17</div></a></div><div class="data-item"><a href="/oct25-xxxxx/categories/"><div class="headline">分类</div><div class="length-num">8</div></a></div></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/oct25-xxxxx/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/oct25-xxxxx/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/oct25-xxxxx/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/oct25-xxxxx/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/oct25-xxxxx/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('/oct25-xxxxx/img/top_img/top_bg11.jpg')"><nav id="nav"><span id="blog_name"><a id="site-name" href="/oct25-xxxxx/">Oct25-X</a></span><div id="menus"><div id="search-button"><a class="site-page social-icon search"><i class="fas fa-search fa-fw"></i><span> 搜索</span></a></div><div class="menus_items"><div class="menus_item"><a class="site-page" href="/oct25-xxxxx/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/oct25-xxxxx/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/oct25-xxxxx/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/oct25-xxxxx/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/oct25-xxxxx/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">开源应用容器引擎Docker_03常用组件Docker安装</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2023-11-22T01:50:13.000Z" title="发表于 2023-11-22 09:50:13">2023-11-22</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2024-02-17T14:32:50.588Z" title="更新于 2024-02-17 22:32:50">2024-02-17</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/oct25-xxxxx/categories/%E5%AE%B9%E5%99%A8%E5%8C%96/">容器化</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="开源应用容器引擎Docker_03常用组件Docker安装"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><h1 id="Docer-安装整体步骤"><a href="#Docer-安装整体步骤" class="headerlink" title="Docer 安装整体步骤"></a>Docer 安装整体步骤</h1><ol>
<li>搜索镜像</li>
<li>拉取镜像</li>
<li>查看镜像</li>
<li>启动镜像<ul>
<li>服务端口映射</li>
</ul>
</li>
<li>停止容器</li>
<li>移除容器</li>
</ol>
<h1 id="单机部署-MySQL-x2F-Redis"><a href="#单机部署-MySQL-x2F-Redis" class="headerlink" title="单机部署 MySQL&#x2F;Redis"></a>单机部署 MySQL&#x2F;Redis</h1><h2 id="一、安装-MySQL8"><a href="#一、安装-MySQL8" class="headerlink" title="一、安装 MySQL8"></a>一、安装 MySQL8</h2><h3 id="安装步骤"><a href="#安装步骤" class="headerlink" title="安装步骤"></a>安装步骤</h3><ol>
<li><strong>拉取镜像</strong></li>
</ol>
<figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">docker pull mysql:8.0</span><br></pre></td></tr></table></figure>

<ol start="2">
<li><strong>查看镜像</strong></li>
</ol>
<figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">[root@hadoop100 ~]$ docker images</span><br><span class="line">REPOSITORY              TAG       IMAGE ID       CREATED         SIZE</span><br><span class="line">tomcat                  latest    fb5657adc892   10 months ago   680MB</span><br><span class="line">mysql                   8.0       3218b38490ce   10 months ago   516MB</span><br><span class="line">registry                latest    b8604a3fe854   12 months ago   26.2MB</span><br><span class="line">ubuntu                  latest    ba6acccedd29   13 months ago   72.8MB</span><br><span class="line">hello-world             latest    feb5d9fea6a5   13 months ago   13.3kB</span><br><span class="line">billygoo/tomcat8-jdk8   latest    30ef4019761d   4 years ago     523MB</span><br></pre></td></tr></table></figure>

<ol start="3">
<li><strong>启动镜像（或创建容器实例）</strong></li>
</ol>
<p><code>注意：在启动之前，需要查看一下宿主机是否已经启动过 mysql 服务</code></p>
<p>宿主机已经有 mysql 服务启动了，要么把服务停止，要么启动 MySQL 镜像时，端口映射时，宿主机不要使用 3306</p>
<figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#没有启动mysql</span></span><br><span class="line">[root@hadoop100 ~]$ ps -ef | grep mysql</span><br><span class="line">root       4275   2532  0 09:44 pts/0    00:00:00 grep --color=auto mysql</span><br><span class="line"></span><br><span class="line"><span class="comment">#启动镜像</span></span><br><span class="line">[root@hadoop100 ~]$ docker run -p 3306:3306 --name mysql8 -e MYSQL_ROOT_PASSWORD=123456 -d mysql:8.0</span><br><span class="line">9143f0bfd41351956f61d74497a151a3836ade25e66960b3f180ad12747f9030</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">[root@hadoop100 ~]$ docker ps</span><br><span class="line">CONTAINER ID   IMAGE       COMMAND                  CREATED         STATUS         PORTS                                                  NAMES</span><br><span class="line">9143f0bfd413   mysql:8.0   <span class="string">&quot;docker-entrypoint.s…&quot;</span>   6 seconds ago   Up 5 seconds   0.0.0.0:3306-&gt;3306/tcp, :::3306-&gt;3306/tcp, 33060/tcp   mysql8</span><br></pre></td></tr></table></figure>

<p>-e 参数：环境，MYSQL_ROOT_PASSWORD 设置 root 密码</p>
<ol start="4">
<li><strong>进入mysql容器实例，连接mysql，建库建表测试</strong></li>
</ol>
<figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#进入mysql容器</span></span><br><span class="line">[root@hadoop100 ~]$ docker <span class="built_in">exec</span> -it 9143f0bfd413 /bin/bash</span><br><span class="line">root@9143f0bfd413:/<span class="comment"># </span></span><br><span class="line"></span><br><span class="line"><span class="comment">#连接mysql</span></span><br><span class="line">root@9143f0bfd413:/<span class="comment"># mysql -uroot -p123456</span></span><br><span class="line">mysql&gt; </span><br><span class="line"></span><br><span class="line"><span class="comment">#建库</span></span><br><span class="line">mysql&gt; create database test1;</span><br><span class="line">Query OK, 1 row affected (0.01 sec)</span><br><span class="line"></span><br><span class="line"><span class="comment">#建表</span></span><br><span class="line">mysql&gt; create table user(<span class="built_in">id</span> int, name varchar(50));</span><br><span class="line">Query OK, 0 rows affected (0.01 sec)</span><br><span class="line"></span><br><span class="line"><span class="comment">#插入数据</span></span><br><span class="line">mysql&gt; insert into user values(1, <span class="string">&#x27;z3&#x27;</span>);</span><br><span class="line">Query OK, 1 row affected (0.00 sec)</span><br><span class="line"></span><br><span class="line"><span class="comment">#查询</span></span><br><span class="line">mysql&gt; select * from user;</span><br><span class="line">+------+------+</span><br><span class="line">| <span class="built_in">id</span>   | name |</span><br><span class="line">+------+------+</span><br><span class="line">|    1 | z3   |</span><br><span class="line">+------+------+</span><br><span class="line">1 row <span class="keyword">in</span> <span class="built_in">set</span> (0.00 sec)</span><br></pre></td></tr></table></figure>

<ol start="5">
<li><strong>在 windows 中使用工具连接mysql，查询看插入数据是否存在</strong></li>
<li><strong>在 windows 中使用工具插入中文数据</strong></li>
</ol>
<figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#插入</span></span><br><span class="line">insert into user values (2, <span class="string">&#x27;李四&#x27;</span>);</span><br></pre></td></tr></table></figure>

<p>注：由于使用的 mysql 客户端工具的不同，对中文可能作了处理，在插入和查询时，没有问题</p>
<p>本示例中使得的客户端工具是 Navicat</p>
<ol start="7">
<li><strong>到 mysql 容器中查询插入的中文数据</strong></li>
</ol>
<figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">mysql&gt; select * from user;</span><br><span class="line">+------+------+</span><br><span class="line">| <span class="built_in">id</span>   | name |</span><br><span class="line">+------+------+</span><br><span class="line">|    1 | z3   |</span><br><span class="line">|    2 | ??   |</span><br><span class="line">+------+------+</span><br><span class="line">2 rows <span class="keyword">in</span> <span class="built_in">set</span> (0.00 sec)</span><br></pre></td></tr></table></figure>

<ol start="8">
<li><strong>查看字符集</strong></li>
</ol>
<figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">mysql&gt; SHOW VARIABLES LIKE <span class="string">&#x27;character%&#x27;</span>;</span><br><span class="line">+--------------------------+--------------------------------+</span><br><span class="line">| Variable_name            | Value                          |</span><br><span class="line">+--------------------------+--------------------------------+</span><br><span class="line">| character_set_client     | latin1                         |</span><br><span class="line">| character_set_connection | latin1                         |</span><br><span class="line">| character_set_database   | utf8mb4                        |</span><br><span class="line">| character_set_filesystem | binary                         |</span><br><span class="line">| character_set_results    | latin1                         |</span><br><span class="line">| character_set_server     | utf8mb4                        |</span><br><span class="line">| character_set_system     | utf8mb3                        |</span><br><span class="line">| character_sets_dir       | /usr/share/mysql-8.0/charsets/ |</span><br><span class="line">+--------------------------+--------------------------------+</span><br><span class="line">8 rows <span class="keyword">in</span> <span class="built_in">set</span> (0.01 sec)</span><br></pre></td></tr></table></figure>

<h3 id="出现的问题"><a href="#出现的问题" class="headerlink" title="出现的问题"></a>出现的问题</h3><ol>
<li>中文乱码问题</li>
<li>在实际开发或生产部署中，需要添加容器数据卷，以防止删除容器数据丢失问题</li>
</ol>
<h3 id="实际部署步骤"><a href="#实际部署步骤" class="headerlink" title="实际部署步骤"></a>实际部署步骤</h3><ol>
<li><strong>使用容器数据卷启动 mysql 容器实例</strong></li>
</ol>
<figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">[root@hadoop100 ~]$ docker run -d -p 3306:3306 --privileged=<span class="literal">true</span> -v /var/docker/mysql/log:/var/log/mysql -v /var/docker/mysql/data:/var/lib/mysql -v /var/docker/mysql/conf:/etc/mysql/conf.d -e MYSQL_ROOT_PASSWORD=123456  --name mysql8 mysql:8.0</span><br><span class="line">4d41953024c3ca995ffb3dadf2836e2e6241c26ca883a3939e08e973de9bdc4e</span><br></pre></td></tr></table></figure>

<p>此时，启动了容器后，就会创建三个容器数据卷，分别保存 mysql 的日志、数据和配置</p>
<ol start="2">
<li><strong>在宿主机创建mysql配置文件</strong></li>
</ol>
<figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#进入到数据卷对应的配置目录下</span></span><br><span class="line">[root@hadoop100 conf]$ <span class="built_in">cd</span> /var/docker/mysql/conf/</span><br><span class="line"></span><br><span class="line"><span class="comment">#创建my.cnf配置文件</span></span><br><span class="line">[root@hadoop100 conf]$ vim my.cnf</span><br><span class="line"><span class="comment">#添加配置</span></span><br><span class="line">[client]</span><br><span class="line">default_character_set=utf8</span><br><span class="line">[mysqld]</span><br><span class="line">collation_server = utf8_general_ci</span><br><span class="line">character_set_server = utf8</span><br></pre></td></tr></table></figure>

<ol start="3">
<li><strong>重启容器</strong></li>
</ol>
<figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#重启</span></span><br><span class="line">[root@hadoop100 conf]$ docker restart mysql8</span><br><span class="line">mysql8</span><br></pre></td></tr></table></figure>

<ol start="4">
<li><strong>进入mysql测试中文乱码问题</strong></li>
</ol>
<figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#以交互方式进入容器</span></span><br><span class="line">[root@hadoop100 ~]$ docker <span class="built_in">exec</span> -it mysql8 /bin/bash</span><br><span class="line"></span><br><span class="line"><span class="comment">#连接mysql</span></span><br><span class="line">root@4d41953024c3:/<span class="comment"># mysql -uroot -p123456</span></span><br><span class="line">mysql: [Warning] Using a password on the <span class="built_in">command</span> line interface can be insecure.</span><br><span class="line">Welcome to the MySQL monitor.  Commands end with ; or \g.</span><br><span class="line">Your MySQL connection <span class="built_in">id</span> is 8</span><br><span class="line">Server version: 8.0.27 MySQL Community Server - GPL</span><br><span class="line"></span><br><span class="line">Copyright (c) 2000, 2021, Oracle and/or its affiliates.</span><br><span class="line"></span><br><span class="line">Oracle is a registered trademark of Oracle Corporation and/or its</span><br><span class="line">affiliates. Other names may be trademarks of their respective</span><br><span class="line">owners.</span><br><span class="line"></span><br><span class="line">Type <span class="string">&#x27;help;&#x27;</span> or <span class="string">&#x27;\h&#x27;</span> <span class="keyword">for</span> <span class="built_in">help</span>. Type <span class="string">&#x27;\c&#x27;</span> to clear the current input statement.</span><br><span class="line"></span><br><span class="line">mysql&gt; </span><br><span class="line"></span><br><span class="line"><span class="comment">#查看字符集</span></span><br><span class="line">mysql&gt; SHOW VARIABLES LIKE <span class="string">&#x27;character%&#x27;</span>;</span><br><span class="line">+--------------------------+--------------------------------+</span><br><span class="line">| Variable_name            | Value                          |</span><br><span class="line">+--------------------------+--------------------------------+</span><br><span class="line">| character_set_client     | utf8mb3                        |</span><br><span class="line">| character_set_connection | utf8mb3                        |</span><br><span class="line">| character_set_database   | utf8mb3                        |</span><br><span class="line">| character_set_filesystem | binary                         |</span><br><span class="line">| character_set_results    | utf8mb3                        |</span><br><span class="line">| character_set_server     | utf8mb3                        |</span><br><span class="line">| character_set_system     | utf8mb3                        |</span><br><span class="line">| character_sets_dir       | /usr/share/mysql-8.0/charsets/ |</span><br><span class="line">+--------------------------+--------------------------------+</span><br><span class="line">8 rows <span class="keyword">in</span> <span class="built_in">set</span> (0.00 sec)</span><br><span class="line"></span><br><span class="line"><span class="comment">#创建数据库</span></span><br><span class="line">mysql&gt; create database test1;</span><br><span class="line">Query OK, 1 row affected (0.00 sec)</span><br><span class="line"></span><br><span class="line">mysql&gt; </span><br><span class="line">mysql&gt; use test1</span><br><span class="line">Database changed</span><br><span class="line"></span><br><span class="line"><span class="comment">#创建表</span></span><br><span class="line">mysql&gt; create table student(<span class="built_in">id</span> int, name varchar(50));</span><br><span class="line">Query OK, 0 rows affected (0.01 sec)</span><br><span class="line"></span><br><span class="line"><span class="comment">#插入数据</span></span><br><span class="line">mysql&gt; insert into student values(1, <span class="string">&#x27;zs&#x27;</span>);</span><br><span class="line">Query OK, 1 row affected (0.02 sec)</span><br><span class="line"></span><br><span class="line"><span class="comment">#查询 </span></span><br><span class="line">mysql&gt; select * from student;</span><br><span class="line">+------+------+</span><br><span class="line">| <span class="built_in">id</span>   | name |</span><br><span class="line">+------+------+</span><br><span class="line">|    1 | zs   |</span><br><span class="line">+------+------+</span><br><span class="line">1 row <span class="keyword">in</span> <span class="built_in">set</span> (0.00 sec)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">#插入中文数据</span></span><br><span class="line">insert into student values(2, <span class="string">&#x27;李四&#x27;</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">#查询</span></span><br><span class="line">mysql&gt; select * from student;</span><br><span class="line">+------+--------+</span><br><span class="line">| <span class="built_in">id</span>   | name   |</span><br><span class="line">+------+--------+</span><br><span class="line">|    1 | zs     |</span><br><span class="line">|    2 | 李四   |</span><br><span class="line">+------+--------+</span><br><span class="line">2 rows <span class="keyword">in</span> <span class="built_in">set</span> (0.00 sec)</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h3 id="思考"><a href="#思考" class="headerlink" title="思考"></a>思考</h3><p>这时，把 msyql 容器给删除，然后重新创建一个 mysql 容器（设置刚才的容器数据卷），这时新创建的 mysql 容器中是否有数据</p>
<figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">[root@hadoop100 ~]$ docker ps</span><br><span class="line">CONTAINER ID   IMAGE       COMMAND                  CREATED          STATUS          PORTS                                                  NAMES</span><br><span class="line">4d41953024c3   mysql:8.0   <span class="string">&quot;docker-entrypoint.s…&quot;</span>   30 minutes ago   Up 25 minutes   0.0.0.0:3306-&gt;3306/tcp, :::3306-&gt;3306/tcp, 33060/tcp   mysql8</span><br><span class="line"></span><br><span class="line"><span class="comment">#删除运行的容器</span></span><br><span class="line">[root@hadoop100 ~]$ docker <span class="built_in">rm</span> -f mysql8</span><br><span class="line">mysql8</span><br></pre></td></tr></table></figure>

<p>重新创建新的容器，注意，挂载数据卷与刚刚创建的路径一致</p>
<figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#创建容器</span></span><br><span class="line">[root@hadoop100 ~]$ docker run -d -p 3306:3306 --privileged=<span class="literal">true</span> -v /var/docker/mysql/log:/var/log/mysql -v /var/docker/mysql/data:/var/lib/mysql -v /var/docker/mysql/conf:/etc/mysql/conf.d -e MYSQL_ROOT_PASSWORD=123456  --name mysql8 mysql:8.0</span><br><span class="line">ec14515cc762d0a092a73565cdfcdbdc5b9ee100a6b1925198c74d205d914380</span><br><span class="line"></span><br><span class="line">[root@hadoop100 ~]$ docker ps</span><br><span class="line">CONTAINER ID   IMAGE       COMMAND                  CREATED              STATUS              PORTS                                                  NAMES</span><br><span class="line">ec14515cc762   mysql:8.0   <span class="string">&quot;docker-entrypoint.s…&quot;</span>   About a minute ago   Up About a minute   0.0.0.0:3306-&gt;3306/tcp, :::3306-&gt;3306/tcp, 33060/tcp   mysql8</span><br><span class="line"></span><br><span class="line"><span class="comment">#进入容器，连接mysql</span></span><br><span class="line">[root@hadoop100 ~]$ docker <span class="built_in">exec</span> -it ec14515cc762 /bin/bash</span><br><span class="line">root@ec14515cc762:/<span class="comment"># </span></span><br><span class="line">root@ec14515cc762:/<span class="comment"># mysql -uroot -p123456</span></span><br><span class="line">mysql: [Warning] Using a password on the <span class="built_in">command</span> line interface can be insecure.</span><br><span class="line">Welcome to the MySQL monitor.  Commands end with ; or \g.</span><br><span class="line">Your MySQL connection <span class="built_in">id</span> is 8</span><br><span class="line">Server version: 8.0.27 MySQL Community Server - GPL</span><br><span class="line"></span><br><span class="line">Copyright (c) 2000, 2021, Oracle and/or its affiliates.</span><br><span class="line"></span><br><span class="line">Oracle is a registered trademark of Oracle Corporation and/or its</span><br><span class="line">affiliates. Other names may be trademarks of their respective</span><br><span class="line">owners.</span><br><span class="line"></span><br><span class="line">Type <span class="string">&#x27;help;&#x27;</span> or <span class="string">&#x27;\h&#x27;</span> <span class="keyword">for</span> <span class="built_in">help</span>. Type <span class="string">&#x27;\c&#x27;</span> to clear the current input statement.</span><br><span class="line"></span><br><span class="line">mysql&gt; </span><br><span class="line"></span><br><span class="line"><span class="comment">#切换到test1 数据库</span></span><br><span class="line">mysql&gt; use test1</span><br><span class="line">Reading table information <span class="keyword">for</span> completion of table and column names</span><br><span class="line">You can turn off this feature to get a quicker startup with -A</span><br><span class="line"></span><br><span class="line">Database changed</span><br><span class="line">mysql&gt; </span><br><span class="line"></span><br><span class="line"><span class="comment">#查询数据</span></span><br><span class="line">mysql&gt; select * from student;</span><br><span class="line">+------+--------+</span><br><span class="line">| <span class="built_in">id</span>   | name   |</span><br><span class="line">+------+--------+</span><br><span class="line">|    1 | zs     |</span><br><span class="line">|    2 | 李四   |</span><br><span class="line">+------+--------+</span><br><span class="line">2 rows <span class="keyword">in</span> <span class="built_in">set</span> (0.00 sec)</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>所以，容器数据卷在实际就用中是非常重要的，在开发部署过程中一定要使用容器数据卷</p>
<h2 id="一、安装-Redis"><a href="#一、安装-Redis" class="headerlink" title="一、安装 Redis"></a>一、安装 Redis</h2><h3 id="安装-redis-7-0-11"><a href="#安装-redis-7-0-11" class="headerlink" title="安装 redis-7.0.11"></a>安装 redis-7.0.11</h3><ol>
<li><strong>拉取镜像</strong></li>
</ol>
<figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">docker pull redis:7.0.11</span><br></pre></td></tr></table></figure>

<ol start="2">
<li><strong>查看镜像</strong></li>
</ol>
<figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">[root@hadoop100 ~]$ docker images</span><br><span class="line">REPOSITORY              TAG       IMAGE ID       CREATED         SIZE</span><br><span class="line">tomcat                  latest    fb5657adc892   10 months ago   680MB</span><br><span class="line">mysql                   8.0       3218b38490ce   10 months ago   516MB</span><br><span class="line">registry                latest    b8604a3fe854   12 months ago   26.2MB</span><br><span class="line">ubuntu                  latest    ba6acccedd29   13 months ago   72.8MB</span><br><span class="line">hello-world             latest    feb5d9fea6a5   13 months ago   13.3kB</span><br><span class="line">redis                   7.0.11     16ecd2772934   2 years ago     104MB</span><br><span class="line">billygoo/tomcat8-jdk8   latest    30ef4019761d   4 years ago     523MB</span><br></pre></td></tr></table></figure>

<ol start="3">
<li><strong>启动镜像（或创建容器实例）</strong></li>
</ol>
<figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">[root@hadoop100 ~]$ docker run -p 6379:6379 -d redis:7.0.11</span><br><span class="line">a1ef3779e833d58372e7676d647980448bbde5a18b1334a02fd2f1b8b49596a3</span><br><span class="line"></span><br><span class="line">[root@hadoop100 ~]$ docker ps</span><br><span class="line">CONTAINER ID   IMAGE         COMMAND                  CREATED          STATUS          PORTS                                       NAMES</span><br><span class="line">a1ef3779e833   redis:7.0.11   <span class="string">&quot;docker-entrypoint.s…&quot;</span>   27 seconds ago   Up 26 seconds   0.0.0.0:6379-&gt;6379/tcp, :::6379-&gt;6379/tcp   naughty_cray</span><br><span class="line"></span><br><span class="line"><span class="comment">#以交互方式进入容器</span></span><br><span class="line">[root@hadoop100 ~]$ docker <span class="built_in">exec</span> -it a1ef3779e833 /bin/bash</span><br><span class="line">root@a1ef3779e833:/data<span class="comment"># </span></span><br><span class="line"></span><br><span class="line"><span class="comment">#连接redis客户端，测试</span></span><br><span class="line">root@a1ef3779e833:/data<span class="comment"># redis-cli </span></span><br><span class="line">127.0.0.1:6379&gt;</span><br><span class="line">127.0.0.1:6379&gt; <span class="built_in">set</span> k1 v1</span><br><span class="line">OK</span><br><span class="line">127.0.0.1:6379&gt; get k1</span><br><span class="line"><span class="string">&quot;v1&quot;</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h3 id="实际部署步骤-1"><a href="#实际部署步骤-1" class="headerlink" title="实际部署步骤"></a>实际部署步骤</h3><ol>
<li><strong>创建目录</strong></li>
</ol>
<figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">mkdir</span> -p /var/docker/redis/conf</span><br></pre></td></tr></table></figure>

<ol start="2">
<li><strong>将 redis.conf 拷贝到目录中</strong></li>
</ol>
<figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">cp</span> redis.conf /var/docker/redis/conf</span><br></pre></td></tr></table></figure>

<p>redis.conf 可以解压一个 Redis，从里找一个配置文件即可</p>
<ol start="3">
<li><strong>修改配置文件</strong></li>
</ol>
<figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#开启 redis 验证（可选）</span></span><br><span class="line">requirepass 123</span><br><span class="line"></span><br><span class="line"><span class="comment">#允许 redis 远程连接（注释掉）</span></span><br><span class="line"><span class="comment">#bind 127.0.0.1</span></span><br><span class="line"></span><br><span class="line"><span class="comment">#将daemonize 设置为 no，或注释</span></span><br><span class="line"><span class="comment">#该配置与 docker run -d 参数冲突，会导致容器启动失败</span></span><br><span class="line">daemonize no</span><br><span class="line"></span><br><span class="line"><span class="comment">#开启redis数据持久化（可选）</span></span><br><span class="line">appendonly <span class="built_in">yes</span></span><br></pre></td></tr></table></figure>

<ol start="4">
<li><strong>创建 redis 容器实例</strong></li>
</ol>
<figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">[root@hadoop100 ~]$ docker run -p 6379:6379 --name redis --privileged=<span class="literal">true</span> -v /var/docker/redis/conf/redis.conf:/etc/redis/redis.conf -v /var/docker/redis/data:/data -d redis:7.0.11 redis-server /etc/redis/redis.conf</span><br><span class="line">e3aed66d937f4c2076e7a8dec12c50bcb44a4015fa620901805a680a3808e3e5</span><br></pre></td></tr></table></figure>

<ol start="5">
<li><strong>使用客户端连接</strong></li>
</ol>
<figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#进入redis容器</span></span><br><span class="line">[root@hadoop100 ~]$ docker <span class="built_in">exec</span> -it redis /bin/bash</span><br><span class="line">root@5452cc47d18f:/data<span class="comment"># </span></span><br><span class="line"></span><br><span class="line"><span class="comment">#客户端连接</span></span><br><span class="line">root@5452cc47d18f:/data<span class="comment"># redis-cli  </span></span><br><span class="line">127.0.0.1:6379&gt;</span><br><span class="line"></span><br><span class="line"><span class="comment">#测试</span></span><br><span class="line">127.0.0.1:6379&gt; <span class="built_in">set</span> k1 v1</span><br><span class="line">OK</span><br><span class="line">127.0.0.1:6379&gt; get k1</span><br><span class="line"><span class="string">&quot;v1&quot;</span></span><br><span class="line">127.0.0.1:6379&gt; </span><br><span class="line">127.0.0.1:6379&gt; ping</span><br><span class="line">PONG</span><br><span class="line"></span><br><span class="line"><span class="comment">#redis 默认16个库，切换库测试</span></span><br><span class="line">127.0.0.1:6379&gt; select 8</span><br><span class="line">OK</span><br><span class="line">127.0.0.1:6379[8]&gt; select 15</span><br><span class="line">OK</span><br></pre></td></tr></table></figure>

<ol start="6">
<li><strong>修改 redis 默认的库数量</strong></li>
</ol>
<p>redis.conf 中，默认设置的 <code>database： 16</code>，默认16个库，修改为10个，测试 redis容器启动使用到了自己的 redis.conf配置文件</p>
<figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#修改前默认为16个库</span></span><br><span class="line"><span class="comment">#database 16</span></span><br><span class="line"></span><br><span class="line"><span class="comment">#修改为10个</span></span><br><span class="line">database 10</span><br></pre></td></tr></table></figure>

<ol start="7">
<li><strong>重启容器</strong></li>
</ol>
<figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">[root@hadoop100 ~]$ docker restart e3aed66d937f</span><br><span class="line">e3aed66d937f</span><br></pre></td></tr></table></figure>

<ol start="8">
<li><strong>连接测试</strong></li>
</ol>
<figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#进入容器</span></span><br><span class="line">[root@hadoop100 ~]$ docker <span class="built_in">exec</span> -it redis /bin/bash</span><br><span class="line">root@e3aed66d937f:/data<span class="comment"># </span></span><br><span class="line"></span><br><span class="line"><span class="comment">#连接redis</span></span><br><span class="line">root@e3aed66d937f:/data<span class="comment"># redis-cli </span></span><br><span class="line">127.0.0.1:6379&gt; </span><br><span class="line">127.0.0.1:6379&gt; get k1</span><br><span class="line"><span class="string">&quot;v1&quot;</span></span><br><span class="line">127.0.0.1:6379&gt; </span><br><span class="line">127.0.0.1:6379&gt; select 8</span><br><span class="line">OK</span><br><span class="line">127.0.0.1:6379[8]&gt; </span><br><span class="line"></span><br><span class="line"><span class="comment">#切换到库16失败</span></span><br><span class="line">127.0.0.1:6379[8]&gt; select 15</span><br><span class="line">(error) ERR DB index is out of range</span><br></pre></td></tr></table></figure>

<h1 id="集群部署-MySQL-x2F-Redis"><a href="#集群部署-MySQL-x2F-Redis" class="headerlink" title="集群部署 MySQL&#x2F;Redis"></a>集群部署 MySQL&#x2F;Redis</h1><h2 id="一、MySQL-主从复制部署"><a href="#一、MySQL-主从复制部署" class="headerlink" title="一、MySQL 主从复制部署"></a>一、MySQL 主从复制部署</h2><h3 id="角色规划"><a href="#角色规划" class="headerlink" title="角色规划"></a>角色规划</h3><p>创建 mysql8-3307（master） 和 mysql8-3308（slave） 两个容器实现主从复制</p>
<p>当 mysql8-3307 插入数据时，mysql8-3308 可以收到相同数据即可</p>
<h3 id="mysql8-3307（master）"><a href="#mysql8-3307（master）" class="headerlink" title="mysql8-3307（master）"></a>mysql8-3307（master）</h3><p>创建容器实例</p>
<figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">[root@hadoop100 ~]$ docker run -d -p 3307:3306 --privileged=<span class="literal">true</span> -v /var/docker/mysql8-3307/log:/var/log/mysql -v /var/docker/mysql8-3307/data:/var/lib/mysql -v /var/docker/mysql8-3307/conf:/etc/mysql/conf.d -e MYSQL_ROOT_PASSWORD=123456  --name mysql8-3307 mysql:8.0</span><br><span class="line">c507c1f0a305d7071fec48d4e0cecfbc9a31069a5bfd78a64b5979b2a008a5fd</span><br><span class="line">[root@hadoop100 ~]$ </span><br><span class="line">[root@hadoop100 ~]$ </span><br><span class="line">[root@hadoop100 ~]$ docker ps</span><br><span class="line">CONTAINER ID   IMAGE       COMMAND                  CREATED         STATUS         PORTS                                                  NAMES</span><br><span class="line">c507c1f0a305   mysql:8.0   <span class="string">&quot;docker-entrypoint.s…&quot;</span>   7 seconds ago   Up 6 seconds   33060/tcp, 0.0.0.0:3307-&gt;3306/tcp, :::3307-&gt;3306/tcp   mysql8-3307</span><br></pre></td></tr></table></figure>

<p>进入到 <code>/var/docker/mysql8-3307/conf</code> 目录下，创建my.cnf，添加配置</p>
<figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line">[mysqld]</span><br><span class="line"><span class="comment">## 设置server_id，同一局域网中需要唯一</span></span><br><span class="line">server_id=101</span><br><span class="line"><span class="comment">## 指定不需要同步的数据库名称</span></span><br><span class="line">binlog-ignore-db=mysql  </span><br><span class="line"><span class="comment">## 开启二进制日志功能</span></span><br><span class="line">log-bin=master-bin  </span><br><span class="line"><span class="comment">## 设置二进制日志使用内存大小（事务）</span></span><br><span class="line">binlog_cache_size=1M  </span><br><span class="line"><span class="comment">## 设置使用的二进制日志格式（mixed,statement,row）</span></span><br><span class="line">binlog_format=mixed  </span><br><span class="line"><span class="comment">## 二进制日志过期清理时间。默认值为0，表示不自动清理。</span></span><br><span class="line">expire_logs_days=7  </span><br><span class="line"><span class="comment">## 跳过主从复制中遇到的所有错误或指定类型的错误，避免slave端复制中断。</span></span><br><span class="line"><span class="comment">## 如：1062错误是指一些主键重复，1032错误是因为主从数据库数据不一致</span></span><br><span class="line">slave_skip_errors=1062</span><br></pre></td></tr></table></figure>

<p>重启容器</p>
<figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">[root@hadoop100 ~]$ docker restart c507c1f0a305</span><br><span class="line">c507c1f0a305</span><br></pre></td></tr></table></figure>

<p>进入容器，连接mysql测试</p>
<figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#进入容器</span></span><br><span class="line">[root@hadoop100 ~]$ docker <span class="built_in">exec</span> -it c507c1f0a305 /bin/bash</span><br><span class="line">root@c507c1f0a305:/<span class="comment"># </span></span><br><span class="line"></span><br><span class="line"><span class="comment">#连接mysql</span></span><br><span class="line">root@c507c1f0a305:/<span class="comment"># mysql -uroot -p123456</span></span><br><span class="line">mysql: [Warning] Using a password on the <span class="built_in">command</span> line interface can be insecure.</span><br><span class="line">Welcome to the MySQL monitor.  Commands end with ; or \g.</span><br><span class="line">Your MySQL connection <span class="built_in">id</span> is 8</span><br><span class="line">Server version: 8.0.27 MySQL Community Server - GPL</span><br><span class="line"></span><br><span class="line">Copyright (c) 2000, 2021, Oracle and/or its affiliates.</span><br><span class="line"></span><br><span class="line">Oracle is a registered trademark of Oracle Corporation and/or its</span><br><span class="line">affiliates. Other names may be trademarks of their respective</span><br><span class="line">owners.</span><br><span class="line"></span><br><span class="line">Type <span class="string">&#x27;help;&#x27;</span> or <span class="string">&#x27;\h&#x27;</span> <span class="keyword">for</span> <span class="built_in">help</span>. Type <span class="string">&#x27;\c&#x27;</span> to clear the current input statement.</span><br><span class="line"></span><br><span class="line">mysql&gt;</span><br></pre></td></tr></table></figure>

<p>在 master 容器实例内，创建数据同步用户</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">--创建一个用户</span></span><br><span class="line"><span class="keyword">CREATE</span> <span class="keyword">USER</span> <span class="string">&#x27;slave&#x27;</span>@<span class="string">&#x27;%&#x27;</span> IDENTIFIED <span class="keyword">WITH</span> <span class="string">&#x27;mysql_native_password&#x27;</span> <span class="keyword">BY</span> <span class="string">&#x27;123456&#x27;</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">--给用户授权</span></span><br><span class="line"><span class="keyword">GRANT</span> REPLICATION SLAVE, REPLICATION CLIENT <span class="keyword">ON</span> <span class="operator">*</span>.<span class="operator">*</span> <span class="keyword">TO</span> <span class="string">&#x27;slave&#x27;</span>@<span class="string">&#x27;%&#x27;</span>;</span><br></pre></td></tr></table></figure>

<h3 id="mysql8-3308（slave）"><a href="#mysql8-3308（slave）" class="headerlink" title="mysql8-3308（slave）"></a>mysql8-3308（slave）</h3><p>创建容器实例</p>
<figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">[root@hadoop100 ~]$ docker run -d -p 3308:3306 --privileged=<span class="literal">true</span> -v /var/docker/mysql8-3308/log:/var/log/mysql -v /var/docker/mysql8-3308/data:/var/lib/mysql -v /var/docker/mysql8-3308/conf:/etc/mysql/conf.d -e MYSQL_ROOT_PASSWORD=123456  --name mysql8-3308 mysql:8.0</span><br><span class="line">fa827cd4bfea5183243f67c81b1555cb155e9e9657daa90c09c1b5ab72bc3250</span><br><span class="line">[root@hadoop100 ~]$ </span><br><span class="line">[root@hadoop100 ~]$ docker ps</span><br><span class="line">CONTAINER ID   IMAGE       COMMAND                  CREATED         STATUS              PORTS                                                  NAMES</span><br><span class="line">fa827cd4bfea   mysql:8.0   <span class="string">&quot;docker-entrypoint.s…&quot;</span>   3 seconds ago   Up 2 seconds        33060/tcp, 0.0.0.0:3308-&gt;3306/tcp, :::3308-&gt;3306/tcp   mysql8-3308</span><br><span class="line">c507c1f0a305   mysql:8.0   <span class="string">&quot;docker-entrypoint.s…&quot;</span>   2 minutes ago   Up About a minute   33060/tcp, 0.0.0.0:3307-&gt;3306/tcp, :::3307-&gt;3306/tcp   mysql8-3307</span><br></pre></td></tr></table></figure>

<p>进入到 <code>/var/docker/mysql8-3308/conf</code> 目录下，创建my.cnf，添加配置</p>
<figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line">[mysqld]</span><br><span class="line"><span class="comment">## 设置server_id，同一局域网中需要唯一</span></span><br><span class="line">server_id=102</span><br><span class="line"><span class="comment">## 指定不需要同步的数据库名称</span></span><br><span class="line">binlog-ignore-db=mysql  </span><br><span class="line"><span class="comment">## 开启二进制日志功能，以备Slave作为其它数据库实例的Master时使用</span></span><br><span class="line">log-bin=mysql-slave-bin  </span><br><span class="line"><span class="comment">## 设置二进制日志使用内存大小（事务）</span></span><br><span class="line">binlog_cache_size=1M  </span><br><span class="line"><span class="comment">## 设置使用的二进制日志格式（mixed,statement,row）</span></span><br><span class="line">binlog_format=mixed  </span><br><span class="line"><span class="comment">## 二进制日志过期清理时间。默认值为0，表示不自动清理。</span></span><br><span class="line">expire_logs_days=7  </span><br><span class="line"><span class="comment">## 跳过主从复制中遇到的所有错误或指定类型的错误，避免slave端复制中断。</span></span><br><span class="line"><span class="comment">## 如：1062错误是指一些主键重复，1032错误是因为主从数据库数据不一致</span></span><br><span class="line">slave_skip_errors=1062  </span><br><span class="line"><span class="comment">## relay_log配置中继日志</span></span><br><span class="line">relay_log=mysql-relay-bin  </span><br><span class="line"><span class="comment">## log_slave_updates表示slave将复制事件写进自己的二进制日志</span></span><br><span class="line">log_slave_updates=1  </span><br><span class="line"><span class="comment">## slave设置为只读（具有super权限的用户除外）</span></span><br><span class="line">read_only=1</span><br></pre></td></tr></table></figure>

<p>重启容器</p>
<figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">[root@hadoop100 ~]$ docker restart fa827cd4bfea</span><br><span class="line">fa827cd4bfea</span><br></pre></td></tr></table></figure>

<p>进入容器，连接mysql测试</p>
<figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line">[root@hadoop100 ~]$ docker <span class="built_in">exec</span> -it fa827cd4bfea /bin/bash</span><br><span class="line">root@fa827cd4bfea:/<span class="comment"># </span></span><br><span class="line">root@fa827cd4bfea:/<span class="comment"># </span></span><br><span class="line">root@fa827cd4bfea:/<span class="comment"># mysql -uroot -p123456</span></span><br><span class="line">mysql: [Warning] Using a password on the <span class="built_in">command</span> line interface can be insecure.</span><br><span class="line">Welcome to the MySQL monitor.  Commands end with ; or \g.</span><br><span class="line">Your MySQL connection <span class="built_in">id</span> is 8</span><br><span class="line">Server version: 8.0.27 MySQL Community Server - GPL</span><br><span class="line"></span><br><span class="line">Copyright (c) 2000, 2021, Oracle and/or its affiliates.</span><br><span class="line"></span><br><span class="line">Oracle is a registered trademark of Oracle Corporation and/or its</span><br><span class="line">affiliates. Other names may be trademarks of their respective</span><br><span class="line">owners.</span><br><span class="line"></span><br><span class="line">Type <span class="string">&#x27;help;&#x27;</span> or <span class="string">&#x27;\h&#x27;</span> <span class="keyword">for</span> <span class="built_in">help</span>. Type <span class="string">&#x27;\c&#x27;</span> to clear the current input statement.</span><br><span class="line"></span><br><span class="line">mysql&gt; </span><br></pre></td></tr></table></figure>

<h3 id="在-master-查看状态（mysql8-3307）"><a href="#在-master-查看状态（mysql8-3307）" class="headerlink" title="在 master 查看状态（mysql8-3307）"></a>在 master 查看状态（mysql8-3307）</h3><figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><span class="line">[root@hadoop100 ~]$ docker ps</span><br><span class="line">CONTAINER ID   IMAGE       COMMAND                  CREATED         STATUS              PORTS                                                  NAMES</span><br><span class="line">fa827cd4bfea   mysql:8.0   <span class="string">&quot;docker-entrypoint.s…&quot;</span>   3 seconds ago   Up 2 seconds        33060/tcp, 0.0.0.0:3308-&gt;3306/tcp, :::3308-&gt;3306/tcp   mysql8-3308</span><br><span class="line">c507c1f0a305   mysql:8.0   <span class="string">&quot;docker-entrypoint.s…&quot;</span>   2 minutes ago   Up About a minute   33060/tcp, 0.0.0.0:3307-&gt;3306/tcp, :::3307-&gt;3306/tcp   mysql8-3307</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">#进入master的容器</span></span><br><span class="line">[root@hadoop100 conf]$ docker <span class="built_in">exec</span> -it c507c1f0a305 /bin/bash</span><br><span class="line">root@c507c1f0a305:/<span class="comment"># </span></span><br><span class="line"></span><br><span class="line"><span class="comment">#连接mysql</span></span><br><span class="line">root@c507c1f0a305:/<span class="comment"># mysql -uroot -p123456</span></span><br><span class="line">mysql: [Warning] Using a password on the <span class="built_in">command</span> line interface can be insecure.</span><br><span class="line">Welcome to the MySQL monitor.  Commands end with ; or \g.</span><br><span class="line">Your MySQL connection <span class="built_in">id</span> is 8</span><br><span class="line">Server version: 8.0.27 MySQL Community Server - GPL</span><br><span class="line"></span><br><span class="line">Copyright (c) 2000, 2021, Oracle and/or its affiliates.</span><br><span class="line"></span><br><span class="line">Oracle is a registered trademark of Oracle Corporation and/or its</span><br><span class="line">affiliates. Other names may be trademarks of their respective</span><br><span class="line">owners.</span><br><span class="line"></span><br><span class="line">Type <span class="string">&#x27;help;&#x27;</span> or <span class="string">&#x27;\h&#x27;</span> <span class="keyword">for</span> <span class="built_in">help</span>. Type <span class="string">&#x27;\c&#x27;</span> to clear the current input statement.</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">#查看状态</span></span><br><span class="line">mysql&gt; show master status;</span><br><span class="line">+-----------------------+----------+--------------+------------------+-------------------+</span><br><span class="line">| File                  | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |</span><br><span class="line">+-----------------------+----------+--------------+------------------+-------------------+</span><br><span class="line">| master-bin.000001 |      681 |              | mysql            |                   |</span><br><span class="line">+-----------------------+----------+--------------+------------------+-------------------+</span><br><span class="line">1 row <span class="keyword">in</span> <span class="built_in">set</span> (0.01 sec)</span><br></pre></td></tr></table></figure>

<h3 id="在-slave-中配置主从复制（mysql8-3308）"><a href="#在-slave-中配置主从复制（mysql8-3308）" class="headerlink" title="在 slave 中配置主从复制（mysql8-3308）"></a>在 slave 中配置主从复制（mysql8-3308）</h3><figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">mysql&gt; change master to master_host=<span class="string">&#x27;192.168.8.100&#x27;</span>, master_user=<span class="string">&#x27;slave&#x27;</span>, master_password=<span class="string">&#x27;123456&#x27;</span>, master_port=3307, master_log_file=<span class="string">&#x27;master-bin.000001&#x27;</span>, master_log_pos=681, master_connect_retry=30;</span><br><span class="line">Query OK, 0 rows affected, 10 warnings (0.03 sec)</span><br></pre></td></tr></table></figure>

<p>参数说明：</p>
<ul>
<li><p>master_host：master 数据库的ip</p>
</li>
<li><p>master_user：master 数据库创建的用于同步数据的用户</p>
</li>
<li><p>master_password：同步用户对应密码</p>
</li>
<li><p>master_port：master 数据库运行端口</p>
</li>
<li><p>master_log_file：指定 slave 数据库要复制数据的日志文件</p>
</li>
<li><p>master_log_pos：指定 slave 数据库从哪个位置开始复制，通过 <code>show master status;</code> 命令 Position 获取</p>
</li>
<li><p>master_connect_retry：连接失败重试的时间</p>
</li>
</ul>
<h3 id="在-slave-中查看主从同步状态（mysql8-3308）"><a href="#在-slave-中查看主从同步状态（mysql8-3308）" class="headerlink" title="在 slave 中查看主从同步状态（mysql8-3308）"></a>在 slave 中查看主从同步状态（mysql8-3308）</h3><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br></pre></td><td class="code"><pre><span class="line">mysql<span class="operator">&gt;</span> <span class="keyword">show</span> slave status \G;</span><br><span class="line"><span class="operator">*</span><span class="operator">*</span><span class="operator">*</span><span class="operator">*</span><span class="operator">*</span><span class="operator">*</span><span class="operator">*</span><span class="operator">*</span><span class="operator">*</span><span class="operator">*</span><span class="operator">*</span><span class="operator">*</span><span class="operator">*</span><span class="operator">*</span><span class="operator">*</span><span class="operator">*</span><span class="operator">*</span><span class="operator">*</span><span class="operator">*</span><span class="operator">*</span><span class="operator">*</span><span class="operator">*</span><span class="operator">*</span><span class="operator">*</span><span class="operator">*</span><span class="operator">*</span><span class="operator">*</span> <span class="number">1.</span> <span class="type">row</span> <span class="operator">*</span><span class="operator">*</span><span class="operator">*</span><span class="operator">*</span><span class="operator">*</span><span class="operator">*</span><span class="operator">*</span><span class="operator">*</span><span class="operator">*</span><span class="operator">*</span><span class="operator">*</span><span class="operator">*</span><span class="operator">*</span><span class="operator">*</span><span class="operator">*</span><span class="operator">*</span><span class="operator">*</span><span class="operator">*</span><span class="operator">*</span><span class="operator">*</span><span class="operator">*</span><span class="operator">*</span><span class="operator">*</span><span class="operator">*</span><span class="operator">*</span><span class="operator">*</span><span class="operator">*</span></span><br><span class="line">               Slave_IO_State: </span><br><span class="line">                  Master_Host: <span class="number">192.168</span><span class="number">.8</span><span class="number">.100</span></span><br><span class="line">                  Master_User: slave</span><br><span class="line">                  Master_Port: <span class="number">3307</span></span><br><span class="line">                Connect_Retry: <span class="number">30</span></span><br><span class="line">              Master_Log_File: master<span class="operator">-</span>bin<span class="number">.000001</span></span><br><span class="line">          Read_Master_Log_Pos: <span class="number">681</span></span><br><span class="line">               Relay_Log_File: mysql<span class="operator">-</span>relay<span class="operator">-</span>bin<span class="number">.000001</span></span><br><span class="line">                Relay_Log_Pos: <span class="number">4</span></span><br><span class="line">        Relay_Master_Log_File: master<span class="operator">-</span>bin<span class="number">.000001</span></span><br><span class="line">             Slave_IO_Running: <span class="keyword">No</span></span><br><span class="line">            Slave_SQL_Running: <span class="keyword">No</span></span><br><span class="line">              Replicate_Do_DB: </span><br><span class="line">          Replicate_Ignore_DB: </span><br><span class="line">           Replicate_Do_Table: </span><br><span class="line">       Replicate_Ignore_Table: </span><br><span class="line">      Replicate_Wild_Do_Table: </span><br><span class="line">  Replicate_Wild_Ignore_Table: </span><br><span class="line">                   Last_Errno: <span class="number">0</span></span><br><span class="line">                   Last_Error: </span><br><span class="line">                 Skip_Counter: <span class="number">0</span></span><br><span class="line">          Exec_Master_Log_Pos: <span class="number">681</span></span><br><span class="line">              Relay_Log_Space: <span class="number">156</span></span><br><span class="line">              Until_Condition: <span class="keyword">None</span></span><br><span class="line">               Until_Log_File: </span><br><span class="line">                Until_Log_Pos: <span class="number">0</span></span><br><span class="line">           Master_SSL_Allowed: <span class="keyword">No</span></span><br><span class="line">           Master_SSL_CA_File: </span><br><span class="line">           Master_SSL_CA_Path: </span><br><span class="line">              Master_SSL_Cert: </span><br><span class="line">            Master_SSL_Cipher: </span><br><span class="line">               Master_SSL_Key: </span><br><span class="line">        Seconds_Behind_Master: <span class="keyword">NULL</span></span><br><span class="line">Master_SSL_Verify_Server_Cert: <span class="keyword">No</span></span><br><span class="line">                Last_IO_Errno: <span class="number">0</span></span><br><span class="line">                Last_IO_Error: </span><br><span class="line">               Last_SQL_Errno: <span class="number">0</span></span><br><span class="line">               Last_SQL_Error: </span><br><span class="line">  Replicate_Ignore_Server_Ids: </span><br><span class="line">             Master_Server_Id: <span class="number">0</span></span><br><span class="line">                  Master_UUID: </span><br><span class="line">             Master_Info_File: mysql.slave_master_info</span><br><span class="line">                    SQL_Delay: <span class="number">0</span></span><br><span class="line">          SQL_Remaining_Delay: <span class="keyword">NULL</span></span><br><span class="line">      Slave_SQL_Running_State: </span><br><span class="line">           Master_Retry_Count: <span class="number">86400</span></span><br><span class="line">                  Master_Bind: </span><br><span class="line">      Last_IO_Error_Timestamp: </span><br><span class="line">     Last_SQL_Error_Timestamp: </span><br><span class="line">               Master_SSL_Crl: </span><br><span class="line">           Master_SSL_Crlpath: </span><br><span class="line">           Retrieved_Gtid_Set: </span><br><span class="line">            Executed_Gtid_Set: </span><br><span class="line">                Auto_Position: <span class="number">0</span></span><br><span class="line">         Replicate_Rewrite_DB: </span><br><span class="line">                 Channel_Name: </span><br><span class="line">           Master_TLS_Version: </span><br><span class="line">       Master_public_key_path: </span><br><span class="line">        Get_master_public_key: <span class="number">0</span></span><br><span class="line">            Network_Namespace: </span><br><span class="line"><span class="number">1</span> <span class="type">row</span> <span class="keyword">in</span> <span class="keyword">set</span>, <span class="number">1</span> warning (<span class="number">0.00</span> sec)</span><br></pre></td></tr></table></figure>

<p><img src="/oct25-xxxxx/img/docker/docker23.png"></p>
<h3 id="在-slave-中开始主从复制（mysql8-3308）"><a href="#在-slave-中开始主从复制（mysql8-3308）" class="headerlink" title="在 slave 中开始主从复制（mysql8-3308）"></a>在 slave 中开始主从复制（mysql8-3308）</h3><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">--开启复制</span></span><br><span class="line">mysql<span class="operator">&gt;</span> <span class="keyword">start</span> slave;</span><br><span class="line">Query OK, <span class="number">0</span> <span class="keyword">rows</span> affected, <span class="number">1</span> warning (<span class="number">0.02</span> sec)</span><br></pre></td></tr></table></figure>

<h3 id="在-slave-中再次查看状态（mysql8-3308）"><a href="#在-slave-中再次查看状态（mysql8-3308）" class="headerlink" title="在 slave 中再次查看状态（mysql8-3308）"></a>在 slave 中再次查看状态（mysql8-3308）</h3><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br></pre></td><td class="code"><pre><span class="line">mysql<span class="operator">&gt;</span> <span class="keyword">show</span> slave status \G;</span><br><span class="line"><span class="operator">*</span><span class="operator">*</span><span class="operator">*</span><span class="operator">*</span><span class="operator">*</span><span class="operator">*</span><span class="operator">*</span><span class="operator">*</span><span class="operator">*</span><span class="operator">*</span><span class="operator">*</span><span class="operator">*</span><span class="operator">*</span><span class="operator">*</span><span class="operator">*</span><span class="operator">*</span><span class="operator">*</span><span class="operator">*</span><span class="operator">*</span><span class="operator">*</span><span class="operator">*</span><span class="operator">*</span><span class="operator">*</span><span class="operator">*</span><span class="operator">*</span><span class="operator">*</span><span class="operator">*</span> <span class="number">1.</span> <span class="type">row</span> <span class="operator">*</span><span class="operator">*</span><span class="operator">*</span><span class="operator">*</span><span class="operator">*</span><span class="operator">*</span><span class="operator">*</span><span class="operator">*</span><span class="operator">*</span><span class="operator">*</span><span class="operator">*</span><span class="operator">*</span><span class="operator">*</span><span class="operator">*</span><span class="operator">*</span><span class="operator">*</span><span class="operator">*</span><span class="operator">*</span><span class="operator">*</span><span class="operator">*</span><span class="operator">*</span><span class="operator">*</span><span class="operator">*</span><span class="operator">*</span><span class="operator">*</span><span class="operator">*</span><span class="operator">*</span></span><br><span class="line">               Slave_IO_State: Waiting <span class="keyword">for</span> source <span class="keyword">to</span> send event</span><br><span class="line">                  Master_Host: <span class="number">192.168</span><span class="number">.8</span><span class="number">.100</span></span><br><span class="line">                  Master_User: slave</span><br><span class="line">                  Master_Port: <span class="number">3307</span></span><br><span class="line">                Connect_Retry: <span class="number">30</span></span><br><span class="line">              Master_Log_File: master<span class="operator">-</span>bin<span class="number">.000001</span></span><br><span class="line">          Read_Master_Log_Pos: <span class="number">681</span></span><br><span class="line">               Relay_Log_File: mysql<span class="operator">-</span>relay<span class="operator">-</span>bin<span class="number">.000002</span></span><br><span class="line">                Relay_Log_Pos: <span class="number">325</span></span><br><span class="line">        Relay_Master_Log_File: master<span class="operator">-</span>bin<span class="number">.000001</span></span><br><span class="line">             Slave_IO_Running: Yes</span><br><span class="line">            Slave_SQL_Running: Yes</span><br><span class="line">              Replicate_Do_DB: </span><br><span class="line">          Replicate_Ignore_DB: </span><br><span class="line">           Replicate_Do_Table: </span><br><span class="line">       Replicate_Ignore_Table: </span><br><span class="line">      Replicate_Wild_Do_Table: </span><br><span class="line">  Replicate_Wild_Ignore_Table: </span><br><span class="line">                   Last_Errno: <span class="number">0</span></span><br><span class="line">                   Last_Error: </span><br><span class="line">                 Skip_Counter: <span class="number">0</span></span><br><span class="line">          Exec_Master_Log_Pos: <span class="number">681</span></span><br><span class="line">              Relay_Log_Space: <span class="number">534</span></span><br><span class="line">              Until_Condition: <span class="keyword">None</span></span><br><span class="line">               Until_Log_File: </span><br><span class="line">                Until_Log_Pos: <span class="number">0</span></span><br><span class="line">           Master_SSL_Allowed: <span class="keyword">No</span></span><br><span class="line">           Master_SSL_CA_File: </span><br><span class="line">           Master_SSL_CA_Path: </span><br><span class="line">              Master_SSL_Cert: </span><br><span class="line">            Master_SSL_Cipher: </span><br><span class="line">               Master_SSL_Key: </span><br><span class="line">        Seconds_Behind_Master: <span class="number">0</span></span><br><span class="line">Master_SSL_Verify_Server_Cert: <span class="keyword">No</span></span><br><span class="line">                Last_IO_Errno: <span class="number">0</span></span><br><span class="line">                Last_IO_Error: </span><br><span class="line">               Last_SQL_Errno: <span class="number">0</span></span><br><span class="line">               Last_SQL_Error: </span><br><span class="line">  Replicate_Ignore_Server_Ids: </span><br><span class="line">             Master_Server_Id: <span class="number">101</span></span><br><span class="line">                  Master_UUID: <span class="number">94</span>ad3085<span class="number">-62</span>f6<span class="number">-11</span>ed<span class="operator">-</span>afb2<span class="number">-0242</span>ac110002</span><br><span class="line">             Master_Info_File: mysql.slave_master_info</span><br><span class="line">                    SQL_Delay: <span class="number">0</span></span><br><span class="line">          SQL_Remaining_Delay: <span class="keyword">NULL</span></span><br><span class="line">      Slave_SQL_Running_State: Replica has read <span class="keyword">all</span> relay log; waiting <span class="keyword">for</span> more updates</span><br><span class="line">           Master_Retry_Count: <span class="number">86400</span></span><br><span class="line">                  Master_Bind: </span><br><span class="line">      Last_IO_Error_Timestamp: </span><br><span class="line">     Last_SQL_Error_Timestamp: </span><br><span class="line">               Master_SSL_Crl: </span><br><span class="line">           Master_SSL_Crlpath: </span><br><span class="line">           Retrieved_Gtid_Set: </span><br><span class="line">            Executed_Gtid_Set: </span><br><span class="line">                Auto_Position: <span class="number">0</span></span><br><span class="line">         Replicate_Rewrite_DB: </span><br><span class="line">                 Channel_Name: </span><br><span class="line">           Master_TLS_Version: </span><br><span class="line">       Master_public_key_path: </span><br><span class="line">        Get_master_public_key: <span class="number">0</span></span><br><span class="line">            Network_Namespace: </span><br><span class="line"><span class="number">1</span> <span class="type">row</span> <span class="keyword">in</span> <span class="keyword">set</span>, <span class="number">1</span> warning (<span class="number">0.00</span> sec)</span><br></pre></td></tr></table></figure>

<p><img src="/oct25-xxxxx/img/docker/docker24.png"></p>
<h3 id="主从复制测试"><a href="#主从复制测试" class="headerlink" title="主从复制测试"></a>主从复制测试</h3><p>在 master 中创建数据库和表，并插入数据</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">--创建数据库</span></span><br><span class="line">mysql<span class="operator">&gt;</span> <span class="keyword">create</span> database test1;</span><br><span class="line">Query OK, <span class="number">1</span> <span class="type">row</span> affected (<span class="number">0.01</span> sec)</span><br><span class="line">mysql<span class="operator">&gt;</span> </span><br><span class="line">mysql<span class="operator">&gt;</span> use test1</span><br><span class="line">Database changed</span><br><span class="line"></span><br><span class="line"><span class="comment">--创建表</span></span><br><span class="line">mysql<span class="operator">&gt;</span> <span class="keyword">create</span> <span class="keyword">table</span> person(id <span class="type">int</span>, name <span class="type">varchar</span>(<span class="number">50</span>));</span><br><span class="line">Query OK, <span class="number">0</span> <span class="keyword">rows</span> affected (<span class="number">0.01</span> sec)</span><br><span class="line"></span><br><span class="line"><span class="comment">--插入数据</span></span><br><span class="line">mysql<span class="operator">&gt;</span> <span class="keyword">insert</span> <span class="keyword">into</span> person <span class="keyword">values</span> (<span class="number">1</span>, <span class="string">&#x27;z3&#x27;</span>);</span><br><span class="line">Query OK, <span class="number">1</span> <span class="type">row</span> affected (<span class="number">0.02</span> sec)</span><br><span class="line"></span><br><span class="line"><span class="comment">--查询</span></span><br><span class="line">mysql<span class="operator">&gt;</span> <span class="keyword">select</span> <span class="operator">*</span> <span class="keyword">from</span> person;</span><br><span class="line"><span class="operator">+</span><span class="comment">------+------+</span></span><br><span class="line"><span class="operator">|</span> id   <span class="operator">|</span> name <span class="operator">|</span></span><br><span class="line"><span class="operator">+</span><span class="comment">------+------+</span></span><br><span class="line"><span class="operator">|</span>    <span class="number">1</span> <span class="operator">|</span> z3   <span class="operator">|</span></span><br><span class="line"><span class="operator">+</span><span class="comment">------+------+</span></span><br><span class="line"><span class="number">1</span> <span class="type">row</span> <span class="keyword">in</span> <span class="keyword">set</span> (<span class="number">0.00</span> sec)</span><br></pre></td></tr></table></figure>

<p>在 slave 中查询，看是否有对应数据</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line">mysql<span class="operator">&gt;</span> <span class="keyword">show</span> databases;</span><br><span class="line"><span class="operator">+</span><span class="comment">--------------------+</span></span><br><span class="line"><span class="operator">|</span> Database           <span class="operator">|</span></span><br><span class="line"><span class="operator">+</span><span class="comment">--------------------+</span></span><br><span class="line"><span class="operator">|</span> information_schema <span class="operator">|</span></span><br><span class="line"><span class="operator">|</span> mysql              <span class="operator">|</span></span><br><span class="line"><span class="operator">|</span> performance_schema <span class="operator">|</span></span><br><span class="line"><span class="operator">|</span> sys                <span class="operator">|</span></span><br><span class="line"><span class="operator">|</span> test1              <span class="operator">|</span></span><br><span class="line"><span class="operator">+</span><span class="comment">--------------------+</span></span><br><span class="line"><span class="number">5</span> <span class="keyword">rows</span> <span class="keyword">in</span> <span class="keyword">set</span> (<span class="number">0.01</span> sec)</span><br><span class="line"></span><br><span class="line">mysql<span class="operator">&gt;</span> </span><br><span class="line">mysql<span class="operator">&gt;</span> use test1;</span><br><span class="line">Reading <span class="keyword">table</span> information <span class="keyword">for</span> completion <span class="keyword">of</span> <span class="keyword">table</span> <span class="keyword">and</span> <span class="keyword">column</span> names</span><br><span class="line">You can turn off this feature <span class="keyword">to</span> <span class="keyword">get</span> a quicker startup <span class="keyword">with</span> <span class="operator">-</span>A</span><br><span class="line"></span><br><span class="line">Database changed</span><br><span class="line">mysql<span class="operator">&gt;</span> </span><br><span class="line">mysql<span class="operator">&gt;</span> <span class="keyword">select</span> <span class="operator">*</span> <span class="keyword">from</span> person;</span><br><span class="line"><span class="operator">+</span><span class="comment">------+------+</span></span><br><span class="line"><span class="operator">|</span> id   <span class="operator">|</span> name <span class="operator">|</span></span><br><span class="line"><span class="operator">+</span><span class="comment">------+------+</span></span><br><span class="line"><span class="operator">|</span>    <span class="number">1</span> <span class="operator">|</span> z3   <span class="operator">|</span></span><br><span class="line"><span class="operator">+</span><span class="comment">------+------+</span></span><br><span class="line"><span class="number">1</span> <span class="type">row</span> <span class="keyword">in</span> <span class="keyword">set</span> (<span class="number">0.00</span> sec)</span><br></pre></td></tr></table></figure>

<h3 id="问题"><a href="#问题" class="headerlink" title="问题"></a>问题</h3><p>slave 使用命令 <code>start slave</code> 开启同步时，再次查看 slave 状态 <code>show slave status \G;</code> 时，显示如下：</p>
<figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">mysql&gt; show slave status \G;</span><br><span class="line">*************************** 1. row ***************************</span><br><span class="line">略... ...</span><br><span class="line">             Slave_IO_Running: Connecting</span><br><span class="line">            Slave_SQL_Running: Yes</span><br><span class="line">略... ...</span><br></pre></td></tr></table></figure>

<p>正常情况下，上面两项都为 yes 时，主从配置才为成功，当前 slave 连接 master 一直处理 Connecting 状态是有问题的，可以从以下点进行排查：</p>
<ol>
<li><p>主从机的网络连接</p>
</li>
<li><p>master ip地址和端口是否正常，需要配置宿主机的ip地址和端口，不是docker映射后的端口</p>
</li>
<li><p>master 创建并授权的用户，是否与从机配置时一致，密码是否正确</p>
</li>
<li><p>binlog 的文件名，以及起始位置是否正确</p>
</li>
</ol>
<figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#对应上面问题2，3，4</span></span><br><span class="line">change master to master_host=<span class="string">&#x27;192.168.8.100&#x27;</span>, master_user=<span class="string">&#x27;slave&#x27;</span>, master_password=<span class="string">&#x27;123456&#x27;</span>, master_port=3307, master_log_file=<span class="string">&#x27;master-bin.000001&#x27;</span>, master_log_pos=681, master_connect_retry=30;</span><br></pre></td></tr></table></figure>

<ol start="5">
<li>mysql 配置文件 my.cnf 中，server_id 需要全局唯一</li>
</ol>
<h2 id="二、Redis-集群"><a href="#二、Redis-集群" class="headerlink" title="二、Redis 集群"></a>二、Redis 集群</h2><h3 id="分布式设计方案"><a href="#分布式设计方案" class="headerlink" title="分布式设计方案"></a>分布式设计方案</h3><h4 id="Hash-取余分区"><a href="#Hash-取余分区" class="headerlink" title="Hash 取余分区"></a>Hash 取余分区</h4><p><img src="/oct25-xxxxx/img/docker/docker25.png"></p>
<p>假设有3台机器构成一个集群，每次读写操作都是根据公式：<code>hash(key) % N台节点</code>，计算出哈希值，用来决定数据映射到哪个节点</p>
<p><strong>优点</strong></p>
<p>简单粗暴，直接有效</p>
<p><strong>缺点</strong></p>
<p>遇到数据量增加，集群资源紧张，需要扩容时，就比较麻烦了。当节点变动时，就需要重新对映射关系进行计算；当某台机器宕机，集群节点数发生变化，然后导致 hash 取余全部数据重新洗牌</p>
<h4 id="一致性-Hash-算法"><a href="#一致性-Hash-算法" class="headerlink" title="一致性 Hash 算法"></a>一致性 Hash 算法</h4><p><strong>主要解决</strong></p>
<p>分布式缓存数据变动和映射问题，某个机器宕机，当<code>hash(key) % N台节点</code> 分母变化时，自然取余数就可以了</p>
<p><strong>目的</strong></p>
<p>当服务器个数发生变动时，尽量减少影响客户端到服务器的映射</p>
<p><strong>三大步骤</strong></p>
<ol>
<li>算法构建一致性哈希环</li>
</ol>
<p><img src="/oct25-xxxxx/img/docker/docker26.png"></p>
<ol start="2">
<li>服务器IP节点映射</li>
</ol>
<p><img src="/oct25-xxxxx/img/docker/docker27.png"></p>
<ol start="3">
<li>key 落到服务器的落键规则</li>
</ol>
<p><img src="/oct25-xxxxx/img/docker/docker28.png"></p>
<p><strong>优点</strong></p>
<p>容错性：当某台节点（如上图C节点）挂了，原本需要落到故障节点的数据，还是会遵循落键规则，越过该故障节点，存储到下一节点（上图的D节点）</p>
<p>扩展性： 需要添加节点时，会根据原有的规则计算，然后得到其在环上的位置，但是有个问题，需要将后面节点到当前插入节点数据重新插入，让数据落到新节点</p>
<p> <strong>缺点</strong></p>
<p>数据倾斜</p>
<h4 id="Hash-槽分区"><a href="#Hash-槽分区" class="headerlink" title="Hash 槽分区"></a>Hash 槽分区</h4><p><strong>是什么</strong></p>
<p>哈希槽实质就是一个数组，数组 [0, 2^14-1] 形成 hash slot 空间</p>
<p><strong>解决问题</strong></p>
<p>解决一致性性哈希算法的数据倾斜的问题</p>
<p><strong>能干什么</strong></p>
<p>解决均匀分配的问题，在数据和节点之间又加入了一层，把这层称为哈希槽（slot），用于管理数据和节点之间的关系，现在就相当于节点上放的是槽，槽里放的是数据</p>
<p><img src="/oct25-xxxxx/img/docker/docker29.png"></p>
<p>槽解决的是粒度问题，相当于把粒度变大了，这样便于数据移动</p>
<p>哈希解决的是映射问题，使用key的哈希值来计算所在的槽，便于数据分配</p>
<p><strong>多少个 hash 槽</strong></p>
<p>一个集群只能有16384个槽，编号0-16383（0-2^14-1）</p>
<p>这些槽会分配给集群中的所有主节点，分配策略没有要求。可以指定哪些编号的槽分配给哪个主节点。集群会记录节点和槽的对应关系</p>
<p>解决了节点和槽的关系后，接下来就需要对key求哈希值，然后对16384取余，余数是几key就落入对应的槽里</p>
<p><code>slot = CRC16(key) % 16384</code></p>
<p>以槽为单位移动数据，因为槽的数目是固定的，处理起来比较容易，这样数据移动问题就解决了</p>
<p><strong>哈希槽计算</strong></p>
<p>Redis 集群中内置了 16384 个哈希槽，redis 会根据节点数量大致均等的将哈希槽映射到不同的节点</p>
<p>当需要在 Redis 集群中放置一个 key-value时，redis 先对 key 使用 crc16 算法算出一个结果，然后把结果对 16384 求余数，这样每个 key 都会对应一个编号在 0-16383 之间的哈希槽，也就是映射到某个节点上</p>
<p><img src="/oct25-xxxxx/img/docker/docker30.png"></p>
<h3 id="三主三从-Redis-集群搭建"><a href="#三主三从-Redis-集群搭建" class="headerlink" title="三主三从 Redis 集群搭建"></a>三主三从 Redis 集群搭建</h3><h4 id="角色分配"><a href="#角色分配" class="headerlink" title="角色分配"></a>角色分配</h4><table>
<thead>
<tr>
<th>主机</th>
<th>端口</th>
<th>从机</th>
<th>端口</th>
</tr>
</thead>
<tbody><tr>
<td>redis-node-1</td>
<td>6381</td>
<td>redis-node-6</td>
<td>6386</td>
</tr>
<tr>
<td>redis-node-2</td>
<td>6382</td>
<td>redis-node-4</td>
<td>6384</td>
</tr>
<tr>
<td>redis-node-3</td>
<td>6383</td>
<td>redis-node-5</td>
<td>6385</td>
</tr>
</tbody></table>
<h4 id="关闭防火墙"><a href="#关闭防火墙" class="headerlink" title="关闭防火墙"></a>关闭防火墙</h4><figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">systemctl stop firewalld</span><br><span class="line"></span><br><span class="line">systemctl status firewalld</span><br></pre></td></tr></table></figure>

<h4 id="启动docker后台服务"><a href="#启动docker后台服务" class="headerlink" title="启动docker后台服务"></a>启动docker后台服务</h4><figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">systemctl start docker</span><br><span class="line"></span><br><span class="line">systemctl status docker</span><br></pre></td></tr></table></figure>

<h4 id="新建6个docker容器实例"><a href="#新建6个docker容器实例" class="headerlink" title="新建6个docker容器实例"></a>新建6个docker容器实例</h4><figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">docker run -d --name redis-node-1 --net host --privileged=<span class="literal">true</span> -v /var/docker/redis-share/redis-node-1:/data redis:7.0.11 --cluster-enabled <span class="built_in">yes</span> --appendonly <span class="built_in">yes</span> --port 6381</span><br><span class="line">docker run -d --name redis-node-2 --net host --privileged=<span class="literal">true</span> -v /var/docker/redis-share/redis-node-2:/data redis:7.0.11 --cluster-enabled <span class="built_in">yes</span> --appendonly <span class="built_in">yes</span> --port 6382</span><br><span class="line">docker run -d --name redis-node-3 --net host --privileged=<span class="literal">true</span> -v /var/docker/redis-share/redis-node-3:/data redis:7.0.11 --cluster-enabled <span class="built_in">yes</span> --appendonly <span class="built_in">yes</span> --port 6383</span><br><span class="line">docker run -d --name redis-node-4 --net host --privileged=<span class="literal">true</span> -v /var/docker/redis-share/redis-node-4:/data redis:7.0.11 --cluster-enabled <span class="built_in">yes</span> --appendonly <span class="built_in">yes</span> --port 6384</span><br><span class="line">docker run -d --name redis-node-5 --net host --privileged=<span class="literal">true</span> -v /var/docker/redis-share/redis-node-5:/data redis:7.0.11 --cluster-enabled <span class="built_in">yes</span> --appendonly <span class="built_in">yes</span> --port 6385</span><br><span class="line">docker run -d --name redis-node-6 --net host --privileged=<span class="literal">true</span> -v /var/docker/redis-share/redis-node-6:/data redis:7.0.11 --cluster-enabled <span class="built_in">yes</span> --appendonly <span class="built_in">yes</span> --port 6386</span><br></pre></td></tr></table></figure>

<p>查看docker进程</p>
<figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">[root@hadoop100 redis]$ docker ps -a</span><br><span class="line">CONTAINER ID   IMAGE         COMMAND                  CREATED         STATUS         PORTS     NAMES</span><br><span class="line">855f68875e92   redis:7.0.11   <span class="string">&quot;docker-entrypoint.s…&quot;</span>   2 minutes ago   Up 2 minutes             redis-node-6</span><br><span class="line">449d89a0b5a3   redis:7.0.11   <span class="string">&quot;docker-entrypoint.s…&quot;</span>   2 minutes ago   Up 2 minutes             redis-node-5</span><br><span class="line">8fbb524c5d04   redis:7.0.11   <span class="string">&quot;docker-entrypoint.s…&quot;</span>   2 minutes ago   Up 2 minutes             redis-node-4</span><br><span class="line">dce9ed2a3928   redis:7.0.11   <span class="string">&quot;docker-entrypoint.s…&quot;</span>   2 minutes ago   Up 2 minutes             redis-node-3</span><br><span class="line">54a53ea6e7c9   redis:7.0.11   <span class="string">&quot;docker-entrypoint.s…&quot;</span>   2 minutes ago   Up 2 minutes             redis-node-2</span><br><span class="line">14fcd03871a7   redis:7.0.11   <span class="string">&quot;docker-entrypoint.s…&quot;</span>   2 minutes ago   Up 2 minutes             redis-node-1</span><br></pre></td></tr></table></figure>

<p>参数说明：</p>
<ul>
<li>–net host：使用宿主机IP和端口，默认</li>
<li>–cluster-enabled yes：开启 redis 集群</li>
<li>–appendonly yes：开启持久化</li>
</ul>
<h4 id="进入redis-node-1-容器"><a href="#进入redis-node-1-容器" class="headerlink" title="进入redis-node-1 容器"></a>进入redis-node-1 容器</h4><figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">[root@hadoop100 redis]$ docker <span class="built_in">exec</span> -it redis-node-1 /bin/bash</span><br><span class="line">root@hadoop100:/data<span class="comment"># </span></span><br></pre></td></tr></table></figure>

<h4 id="构建主从关系"><a href="#构建主从关系" class="headerlink" title="构建主从关系"></a>构建主从关系</h4><p>注意：通过命令构建主从关系时，从节点是随机分配给主节点的</p>
<figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">root@hadoop100:/data<span class="comment"># redis-cli --cluster create 192.168.8.100:6381 192.168.8.100:6382 192.168.8.100:6383 192.168.8.100:6384 192.168.8.100:6385 192.168.8.100:6386 --cluster-replicas 1</span></span><br></pre></td></tr></table></figure>

<p>执行命令，输出结果：</p>
<figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br></pre></td><td class="code"><pre><span class="line">&gt;&gt;&gt; &gt;&gt;&gt; Performing <span class="built_in">hash</span> slots allocation on 6 nodes...</span><br><span class="line">Master[0] -&gt; Slots 0 - 5460</span><br><span class="line">Master[1] -&gt; Slots 5461 - 10922</span><br><span class="line">Master[2] -&gt; Slots 10923 - 16383												<span class="comment">#由于是三主三从模式，所以将redis的16384槽位平均分配到三个主节点</span></span><br><span class="line">Adding replica 192.168.8.100:6385 to 192.168.8.100:6381</span><br><span class="line">Adding replica 192.168.8.100:6386 to 192.168.8.100:6382</span><br><span class="line">Adding replica 192.168.8.100:6384 to 192.168.8.100:6383							<span class="comment">#将三个从节点分配到主节点，可以看到对应关系</span></span><br><span class="line">&gt;&gt;&gt; Trying to optimize slaves allocation <span class="keyword">for</span> anti-affinity</span><br><span class="line">[WARNING] Some slaves are <span class="keyword">in</span> the same host as their master</span><br><span class="line">M: 58a83b9597424d416edd7238d4a7ef7b88cba1d8 192.168.8.100:6381</span><br><span class="line">   slots:[0-5460] (5461 slots) master</span><br><span class="line">M: 5cd3f97b475d86589d8753dbc3d68879b9506d06 192.168.8.100:6382</span><br><span class="line">   slots:[5461-10922] (5462 slots) master</span><br><span class="line">M: 032776c9283be663a09dc31e101c84cca9972fb3 192.168.8.100:6383</span><br><span class="line">   slots:[10923-16383] (5461 slots) master</span><br><span class="line">S: 85a2543184a999a01113c93de0614e3cfa40f13f 192.168.8.100:6384</span><br><span class="line">   replicates 5cd3f97b475d86589d8753dbc3d68879b9506d06</span><br><span class="line">S: 7829c3a9db5d9d7e8c01946bd5498a36e1a50e9e 192.168.8.100:6385</span><br><span class="line">   replicates 032776c9283be663a09dc31e101c84cca9972fb3</span><br><span class="line">S: 4d65747f21c6fc491eda4c2af22be3343902577a 192.168.8.100:6386</span><br><span class="line">   replicates 58a83b9597424d416edd7238d4a7ef7b88cba1d8							<span class="comment">#这里可以看到槽位分配，主从节点对应IP，以及从节点对应主节点关系</span></span><br><span class="line">Can I <span class="built_in">set</span> the above configuration? (<span class="built_in">type</span> <span class="string">&#x27;yes&#x27;</span> to accept): <span class="built_in">yes</span>					<span class="comment">#同意上面分配，需要手动输入yes，然后开始分配</span></span><br><span class="line">&gt;&gt;&gt; Nodes configuration updated</span><br><span class="line">&gt;&gt;&gt; Assign a different config epoch to each node</span><br><span class="line">&gt;&gt;&gt; Sending CLUSTER MEET messages to <span class="built_in">join</span> the cluster</span><br><span class="line">Waiting <span class="keyword">for</span> the cluster to <span class="built_in">join</span></span><br><span class="line">...</span><br><span class="line">&gt;&gt;&gt; Performing Cluster Check (using node 192.168.8.100:6381)</span><br><span class="line">M: 58a83b9597424d416edd7238d4a7ef7b88cba1d8 192.168.8.100:6381</span><br><span class="line">   slots:[0-5460] (5461 slots) master</span><br><span class="line">   1 additional replica(s)</span><br><span class="line">S: 85a2543184a999a01113c93de0614e3cfa40f13f 192.168.8.100:6384</span><br><span class="line">   slots: (0 slots) slave</span><br><span class="line">   replicates 5cd3f97b475d86589d8753dbc3d68879b9506d06</span><br><span class="line">M: 032776c9283be663a09dc31e101c84cca9972fb3 192.168.8.100:6383</span><br><span class="line">   slots:[10923-16383] (5461 slots) master</span><br><span class="line">   1 additional replica(s)</span><br><span class="line">S: 4d65747f21c6fc491eda4c2af22be3343902577a 192.168.8.100:6386</span><br><span class="line">   slots: (0 slots) slave</span><br><span class="line">   replicates 58a83b9597424d416edd7238d4a7ef7b88cba1d8</span><br><span class="line">S: 7829c3a9db5d9d7e8c01946bd5498a36e1a50e9e 192.168.8.100:6385</span><br><span class="line">   slots: (0 slots) slave</span><br><span class="line">   replicates 032776c9283be663a09dc31e101c84cca9972fb3</span><br><span class="line">M: 5cd3f97b475d86589d8753dbc3d68879b9506d06 192.168.8.100:6382</span><br><span class="line">   slots:[5461-10922] (5462 slots) master</span><br><span class="line">   1 additional replica(s)</span><br><span class="line">[OK] All nodes agree about slots configuration.</span><br><span class="line">&gt;&gt;&gt; Check <span class="keyword">for</span> open slots...</span><br><span class="line">&gt;&gt;&gt; Check slots coverage...</span><br><span class="line">[OK] All 16384 slots covered.</span><br></pre></td></tr></table></figure>


<h3 id="连接集群"><a href="#连接集群" class="headerlink" title="连接集群"></a>连接集群</h3><p>连接 Redis 设置数据时，出现 error 的情况，如下：</p>
<figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">root@hadoop100:/data<span class="comment"># redis-cli -p 6381</span></span><br><span class="line">127.0.0.1:6381&gt; </span><br><span class="line">127.0.0.1:6381&gt; </span><br><span class="line">127.0.0.1:6381&gt; <span class="built_in">set</span> k1 v1</span><br><span class="line">(error) MOVED 12706 192.168.8.100:6383</span><br><span class="line">127.0.0.1:6381&gt; </span><br><span class="line">127.0.0.1:6381&gt; <span class="built_in">set</span> k2 v2</span><br><span class="line">OK</span><br><span class="line">127.0.0.1:6381&gt; </span><br><span class="line">127.0.0.1:6381&gt; <span class="built_in">set</span> k3 v3</span><br><span class="line">OK</span><br><span class="line">127.0.0.1:6381&gt; </span><br><span class="line">127.0.0.1:6381&gt; </span><br><span class="line">127.0.0.1:6381&gt; <span class="built_in">set</span> k4 v4</span><br><span class="line">(error) MOVED 8455 192.168.8.100:6382</span><br></pre></td></tr></table></figure>

<p>这是由于我们现在为集群环境，使用命令 <code>redis-cli -p 6381</code> 连接时，为单机连接，并非连接到集群</p>
<p>所以，在 key 超出当前节点槽位时，会出现 error 的情况</p>
<p>解决方法，就是在连接时，添加 -c 参数，通过 6381 连接到集群环境，如下：</p>
<figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">root@hadoop100:/data<span class="comment"># redis-cli -p 6381 -c</span></span><br><span class="line">127.0.0.1:6381&gt; </span><br><span class="line">127.0.0.1:6381&gt; keys *</span><br><span class="line">(empty array)</span><br><span class="line">127.0.0.1:6381&gt; </span><br><span class="line">127.0.0.1:6381&gt; </span><br><span class="line">127.0.0.1:6381&gt; <span class="built_in">set</span> k1 v1</span><br><span class="line">-&gt; Redirected to slot [12706] located at 192.168.8.100:6383</span><br><span class="line">OK</span><br><span class="line">192.168.8.100:6383&gt; <span class="built_in">set</span> k2 v2</span><br><span class="line">-&gt; Redirected to slot [449] located at 192.168.8.100:6381</span><br><span class="line">OK</span><br><span class="line">192.168.8.100:6381&gt; <span class="built_in">set</span> k3 v3</span><br><span class="line">OK</span><br><span class="line">192.168.8.100:6381&gt; <span class="built_in">set</span> k4 v5</span><br><span class="line">-&gt; Redirected to slot [8455] located at 192.168.8.100:6382</span><br><span class="line">OK</span><br></pre></td></tr></table></figure>

<p>在添加 -c 参数后，使我们直接连接到当前 Redis 集群，这样可以根据 key 计算出槽位，然后可以自动节的到对应主机上插入对应 KV</p>
<h3 id="连接信息查看集群信息"><a href="#连接信息查看集群信息" class="headerlink" title="连接信息查看集群信息"></a>连接信息查看集群信息</h3><h4 id="进入6381查看集群状态"><a href="#进入6381查看集群状态" class="headerlink" title="进入6381查看集群状态"></a>进入6381查看集群状态</h4><figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line">root@hadoop100:/data<span class="comment"># redis-cli -p 6381</span></span><br><span class="line">127.0.0.1:6381&gt; </span><br><span class="line">127.0.0.1:6381&gt; cluster info</span><br><span class="line">cluster_state:ok</span><br><span class="line">cluster_slots_assigned:16384</span><br><span class="line">cluster_slots_ok:16384</span><br><span class="line">cluster_slots_pfail:0</span><br><span class="line">cluster_slots_fail:0</span><br><span class="line">cluster_known_nodes:6</span><br><span class="line">cluster_size:3</span><br><span class="line">cluster_current_epoch:6</span><br><span class="line">cluster_my_epoch:1</span><br><span class="line">cluster_stats_messages_ping_sent:207</span><br><span class="line">cluster_stats_messages_pong_sent:209</span><br><span class="line">cluster_stats_messages_sent:416</span><br><span class="line">cluster_stats_messages_ping_received:204</span><br><span class="line">cluster_stats_messages_pong_received:207</span><br><span class="line">cluster_stats_messages_meet_received:5</span><br><span class="line">cluster_stats_messages_received:416</span><br><span class="line">127.0.0.1:6381&gt; </span><br></pre></td></tr></table></figure>

<h4 id="查看节点信息"><a href="#查看节点信息" class="headerlink" title="查看节点信息"></a>查看节点信息</h4><figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">127.0.0.1:6381&gt; cluster nodes</span><br><span class="line">85a2543184a999a01113c93de0614e3cfa40f13f 192.168.8.100:6384@16384 slave 5cd3f97b475d86589d8753dbc3d68879b9506d06 0 1668432480000 2 connected</span><br><span class="line">032776c9283be663a09dc31e101c84cca9972fb3 192.168.8.100:6383@16383 master - 0 1668432481000 3 connected 10923-16383</span><br><span class="line">58a83b9597424d416edd7238d4a7ef7b88cba1d8 192.168.8.100:6381@16381 myself,master - 0 1668432479000 1 connected 0-5460</span><br><span class="line">4d65747f21c6fc491eda4c2af22be3343902577a 192.168.8.100:6386@16386 slave 58a83b9597424d416edd7238d4a7ef7b88cba1d8 0 1668432481502 1 connected</span><br><span class="line">7829c3a9db5d9d7e8c01946bd5498a36e1a50e9e 192.168.8.100:6385@16385 slave 032776c9283be663a09dc31e101c84cca9972fb3 0 1668432480000 3 connected</span><br><span class="line">5cd3f97b475d86589d8753dbc3d68879b9506d06 192.168.8.100:6382@16382 master - 0 1668432480000 2 connected 5461-10922</span><br></pre></td></tr></table></figure>

<h4 id="查看集群信息"><a href="#查看集群信息" class="headerlink" title="查看集群信息"></a>查看集群信息</h4><figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line">root@hadoop100:/data<span class="comment"># redis-cli --cluster check 192.168.8.100:6381</span></span><br><span class="line">192.168.8.100:6381 (58a83b95...) -&gt; 2 keys | 5461 slots | 1 slaves.</span><br><span class="line">192.168.8.100:6383 (032776c9...) -&gt; 1 keys | 5461 slots | 1 slaves.</span><br><span class="line">192.168.8.100:6382 (5cd3f97b...) -&gt; 1 keys | 5462 slots | 1 slaves.</span><br><span class="line">[OK] 4 keys <span class="keyword">in</span> 3 masters.</span><br><span class="line">0.00 keys per slot on average.</span><br><span class="line">&gt;&gt;&gt; Performing Cluster Check (using node 192.168.8.100:6381)</span><br><span class="line">M: 58a83b9597424d416edd7238d4a7ef7b88cba1d8 192.168.8.100:6381</span><br><span class="line">   slots:[0-5460] (5461 slots) master</span><br><span class="line">   1 additional replica(s)</span><br><span class="line">S: 85a2543184a999a01113c93de0614e3cfa40f13f 192.168.8.100:6384</span><br><span class="line">   slots: (0 slots) slave</span><br><span class="line">   replicates 5cd3f97b475d86589d8753dbc3d68879b9506d06</span><br><span class="line">M: 032776c9283be663a09dc31e101c84cca9972fb3 192.168.8.100:6383</span><br><span class="line">   slots:[10923-16383] (5461 slots) master</span><br><span class="line">   1 additional replica(s)</span><br><span class="line">S: 4d65747f21c6fc491eda4c2af22be3343902577a 192.168.8.100:6386</span><br><span class="line">   slots: (0 slots) slave</span><br><span class="line">   replicates 58a83b9597424d416edd7238d4a7ef7b88cba1d8</span><br><span class="line">S: 7829c3a9db5d9d7e8c01946bd5498a36e1a50e9e 192.168.8.100:6385</span><br><span class="line">   slots: (0 slots) slave</span><br><span class="line">   replicates 032776c9283be663a09dc31e101c84cca9972fb3</span><br><span class="line">M: 5cd3f97b475d86589d8753dbc3d68879b9506d06 192.168.8.100:6382</span><br><span class="line">   slots:[5461-10922] (5462 slots) master</span><br><span class="line">   1 additional replica(s)</span><br><span class="line">[OK] All nodes agree about slots configuration.</span><br><span class="line">&gt;&gt;&gt; Check <span class="keyword">for</span> open slots...</span><br><span class="line">&gt;&gt;&gt; Check slots coverage...</span><br><span class="line">[OK] All 16384 slots covered.</span><br></pre></td></tr></table></figure>

<h3 id="容错切换迁移"><a href="#容错切换迁移" class="headerlink" title="容错切换迁移"></a>容错切换迁移</h3><p>我们现在搭建的为三主三从的 Redis 集群，所以集群可以允许三台节点故障，然后会自动将从机切换为主机，可以通过将主机停止来测试集群的容错</p>
<h4 id="停止-redis-node-1"><a href="#停止-redis-node-1" class="headerlink" title="停止 redis-node-1"></a>停止 redis-node-1</h4><p>当前集群，redis-node-1 为主节点，redis-node-6 为其从节点，当前示例，将 redis-node-1 杀死然后查看 redis-node-6是否可以切换为主节点</p>
<figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line">[root@hadoop100 ~]$ docker ps</span><br><span class="line">CONTAINER ID   IMAGE         COMMAND                  CREATED          STATUS          PORTS     NAMES</span><br><span class="line">855f68875e92   redis:7.0.11   <span class="string">&quot;docker-entrypoint.s…&quot;</span>   44 minutes ago   Up 44 minutes             redis-node-6</span><br><span class="line">449d89a0b5a3   redis:7.0.11   <span class="string">&quot;docker-entrypoint.s…&quot;</span>   44 minutes ago   Up 44 minutes             redis-node-5</span><br><span class="line">8fbb524c5d04   redis:7.0.11   <span class="string">&quot;docker-entrypoint.s…&quot;</span>   44 minutes ago   Up 44 minutes             redis-node-4</span><br><span class="line">dce9ed2a3928   redis:7.0.11   <span class="string">&quot;docker-entrypoint.s…&quot;</span>   44 minutes ago   Up 44 minutes             redis-node-3</span><br><span class="line">54a53ea6e7c9   redis:7.0.11   <span class="string">&quot;docker-entrypoint.s…&quot;</span>   44 minutes ago   Up 44 minutes             redis-node-2</span><br><span class="line">14fcd03871a7   redis:7.0.11   <span class="string">&quot;docker-entrypoint.s…&quot;</span>   44 minutes ago   Up 44 minutes             redis-node-1</span><br><span class="line">[root@hadoop100 ~]$ </span><br><span class="line"></span><br><span class="line"><span class="comment">#停止 redis-node-1</span></span><br><span class="line">[root@hadoop100 ~]$ docker stop redis-node-1</span><br><span class="line">redis-node-1</span><br><span class="line">[root@hadoop100 ~]$ </span><br><span class="line"></span><br><span class="line"><span class="comment">#查看docker进程</span></span><br><span class="line">[root@hadoop100 ~]$ docker ps</span><br><span class="line">CONTAINER ID   IMAGE         COMMAND                  CREATED          STATUS          PORTS     NAMES</span><br><span class="line">855f68875e92   redis:7.0.11   <span class="string">&quot;docker-entrypoint.s…&quot;</span>   46 minutes ago   Up 46 minutes             redis-node-6</span><br><span class="line">449d89a0b5a3   redis:7.0.11   <span class="string">&quot;docker-entrypoint.s…&quot;</span>   46 minutes ago   Up 46 minutes             redis-node-5</span><br><span class="line">8fbb524c5d04   redis:7.0.11   <span class="string">&quot;docker-entrypoint.s…&quot;</span>   46 minutes ago   Up 46 minutes             redis-node-4</span><br><span class="line">dce9ed2a3928   redis:7.0.11   <span class="string">&quot;docker-entrypoint.s…&quot;</span>   46 minutes ago   Up 46 minutes             redis-node-3</span><br><span class="line">54a53ea6e7c9   redis:7.0.11   <span class="string">&quot;docker-entrypoint.s…&quot;</span>   46 minutes ago   Up 46 minutes             redis-node-2</span><br></pre></td></tr></table></figure>

<p>连接 redis-node-2，查看集群节点信息</p>
<figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line">[root@hadoop100 redis]$ docker <span class="built_in">exec</span> -it redis-node-2 /bin/bash</span><br><span class="line">root@hadoop100:/data<span class="comment"># </span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">#查看集群节点信息</span></span><br><span class="line">root@hadoop100:/data<span class="comment"># redis-cli -p 6382 -c</span></span><br><span class="line">127.0.0.1:6382&gt; </span><br><span class="line">127.0.0.1:6382&gt; </span><br><span class="line">127.0.0.1:6382&gt; cluster nodes</span><br><span class="line">4d65747f21c6fc491eda4c2af22be3343902577a 192.168.8.100:6386@16386 master - 0 1668434465000 7 connected 0-5460</span><br><span class="line">5cd3f97b475d86589d8753dbc3d68879b9506d06 192.168.8.100:6382@16382 myself,master - 0 1668434467000 2 connected 5461-10922</span><br><span class="line">85a2543184a999a01113c93de0614e3cfa40f13f 192.168.8.100:6384@16384 slave 5cd3f97b475d86589d8753dbc3d68879b9506d06 0 1668434468000 2 connected</span><br><span class="line">032776c9283be663a09dc31e101c84cca9972fb3 192.168.8.100:6383@16383 master - 0 1668434467387 3 connected 10923-16383</span><br><span class="line">7829c3a9db5d9d7e8c01946bd5498a36e1a50e9e 192.168.8.100:6385@16385 slave 032776c9283be663a09dc31e101c84cca9972fb3 0 1668434468400 3 connected</span><br><span class="line">58a83b9597424d416edd7238d4a7ef7b88cba1d8 192.168.8.100:6381@16381 master,fail - 1668434378686 1668434374000 1 disconnected</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">#刚添加了KV还是可以正常获取 </span></span><br><span class="line">127.0.0.1:6382&gt; get k1</span><br><span class="line">-&gt; Redirected to slot [12706] located at 192.168.8.100:6383</span><br><span class="line"><span class="string">&quot;v1&quot;</span></span><br><span class="line">192.168.8.100:6383&gt; </span><br><span class="line">192.168.8.100:6383&gt; get k2</span><br><span class="line">-&gt; Redirected to slot [449] located at 192.168.8.100:6386</span><br><span class="line"><span class="string">&quot;v2&quot;</span></span><br><span class="line">192.168.8.100:6386&gt; </span><br><span class="line">192.168.8.100:6386&gt; get k3</span><br><span class="line"><span class="string">&quot;v3&quot;</span></span><br><span class="line">192.168.8.100:6386&gt; </span><br><span class="line">192.168.8.100:6386&gt; get k4</span><br><span class="line">-&gt; Redirected to slot [8455] located at 192.168.8.100:6382</span><br><span class="line"><span class="string">&quot;v5&quot;</span></span><br></pre></td></tr></table></figure>

<p>redis-node-6 成为主节点，并且可以看到 redis-node-1 还是 master，但是 fail 状态了</p>
<h4 id="重新重启-redis-node-1"><a href="#重新重启-redis-node-1" class="headerlink" title="重新重启 redis-node-1"></a>重新重启 redis-node-1</h4><figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line">[root@hadoop100 ~]$ docker start redis-node-1</span><br><span class="line">redis-node-1</span><br><span class="line">[root@hadoop100 ~]$ </span><br><span class="line">[root@hadoop100 ~]$ docker ps</span><br><span class="line">CONTAINER ID   IMAGE         COMMAND                  CREATED          STATUS          PORTS     NAMES</span><br><span class="line">855f68875e92   redis:7.0.11   <span class="string">&quot;docker-entrypoint.s…&quot;</span>   52 minutes ago   Up 52 minutes             redis-node-6</span><br><span class="line">449d89a0b5a3   redis:7.0.11   <span class="string">&quot;docker-entrypoint.s…&quot;</span>   52 minutes ago   Up 52 minutes             redis-node-5</span><br><span class="line">8fbb524c5d04   redis:7.0.11   <span class="string">&quot;docker-entrypoint.s…&quot;</span>   52 minutes ago   Up 52 minutes             redis-node-4</span><br><span class="line">dce9ed2a3928   redis:7.0.11   <span class="string">&quot;docker-entrypoint.s…&quot;</span>   52 minutes ago   Up 52 minutes             redis-node-3</span><br><span class="line">54a53ea6e7c9   redis:7.0.11   <span class="string">&quot;docker-entrypoint.s…&quot;</span>   52 minutes ago   Up 52 minutes             redis-node-2</span><br><span class="line">14fcd03871a7   redis:7.0.11   <span class="string">&quot;docker-entrypoint.s…&quot;</span>   52 minutes ago   Up 2 seconds              redis-node-1</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">#连接集群查看节点信息</span></span><br><span class="line">192.168.8.100:6382&gt; cluster nodes</span><br><span class="line">4d65747f21c6fc491eda4c2af22be3343902577a 192.168.8.100:6386@16386 master - 0 1668434743969 7 connected 0-5460</span><br><span class="line">5cd3f97b475d86589d8753dbc3d68879b9506d06 192.168.8.100:6382@16382 myself,master - 0 1668434743000 2 connected 5461-10922</span><br><span class="line">85a2543184a999a01113c93de0614e3cfa40f13f 192.168.8.100:6384@16384 slave 5cd3f97b475d86589d8753dbc3d68879b9506d06 0 1668434744000 2 connected</span><br><span class="line">032776c9283be663a09dc31e101c84cca9972fb3 192.168.8.100:6383@16383 master - 0 1668434742946 3 connected 10923-16383</span><br><span class="line">7829c3a9db5d9d7e8c01946bd5498a36e1a50e9e 192.168.8.100:6385@16385 slave 032776c9283be663a09dc31e101c84cca9972fb3 0 1668434744993 3 connected</span><br><span class="line">58a83b9597424d416edd7238d4a7ef7b88cba1d8 192.168.8.100:6381@16381 slave 4d65747f21c6fc491eda4c2af22be3343902577a 0 1668434743000 7 connected</span><br></pre></td></tr></table></figure>

<p>发现 redis-node-1 为 slaver</p>
<h3 id="主从扩容"><a href="#主从扩容" class="headerlink" title="主从扩容"></a>主从扩容</h3><p>现在集群状态，主从关系还是保持最初的状态</p>
<table>
<thead>
<tr>
<th>主机</th>
<th>端口</th>
<th>从机</th>
<th>端口</th>
</tr>
</thead>
<tbody><tr>
<td>redis-node-1</td>
<td>6381</td>
<td>redis-node-6</td>
<td>6386</td>
</tr>
<tr>
<td>redis-node-2</td>
<td>6382</td>
<td>redis-node-4</td>
<td>6384</td>
</tr>
<tr>
<td>redis-node-3</td>
<td>6383</td>
<td>redis-node-5</td>
<td>6385</td>
</tr>
</tbody></table>
<p>将下面的一主一从节点，添加现有的集群，进行集群的扩容</p>
<table>
<thead>
<tr>
<th>主机</th>
<th>端口</th>
<th>从机</th>
<th>端口</th>
</tr>
</thead>
<tbody><tr>
<td>redis-node-7</td>
<td>6387</td>
<td>redis-node-8</td>
<td>6388</td>
</tr>
</tbody></table>
<p>注意：现有集群中，已经将 16384个 slot 分配完，新添加的节点后，需要重新对 slot 进行分配</p>
<h4 id="启动两个新节点"><a href="#启动两个新节点" class="headerlink" title="启动两个新节点"></a>启动两个新节点</h4><figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">docker run -d --name redis-node-7 --net host --privileged=<span class="literal">true</span> -v /var/docker/redis-share/redis-node-7:/data redis:7.0.11 --cluster-enabled <span class="built_in">yes</span> --appendonly <span class="built_in">yes</span> --port 6387</span><br><span class="line">docker run -d --name redis-node-8 --net host --privileged=<span class="literal">true</span> -v /var/docker/redis-share/redis-node-8:/data redis:7.0.11 --cluster-enabled <span class="built_in">yes</span> --appendonly <span class="built_in">yes</span> --port 6388</span><br></pre></td></tr></table></figure>

<p>查看 docker 进程</p>
<figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">[root@hadoop100 ~]$ docker ps</span><br><span class="line">CONTAINER ID   IMAGE         COMMAND                  CREATED             STATUS             PORTS     NAMES</span><br><span class="line">025d0665f957   redis:7.0.11   <span class="string">&quot;docker-entrypoint.s…&quot;</span>   6 seconds ago       Up 5 seconds                 redis-node-8</span><br><span class="line">3a621f23485c   redis:7.0.11   <span class="string">&quot;docker-entrypoint.s…&quot;</span>   6 seconds ago       Up 6 seconds                 redis-node-7</span><br><span class="line">855f68875e92   redis:7.0.11   <span class="string">&quot;docker-entrypoint.s…&quot;</span>   About an hour ago   Up 10 minutes                redis-node-6</span><br><span class="line">449d89a0b5a3   redis:7.0.11   <span class="string">&quot;docker-entrypoint.s…&quot;</span>   About an hour ago   Up About an hour             redis-node-5</span><br><span class="line">8fbb524c5d04   redis:7.0.11   <span class="string">&quot;docker-entrypoint.s…&quot;</span>   About an hour ago   Up About an hour             redis-node-4</span><br><span class="line">dce9ed2a3928   redis:7.0.11   <span class="string">&quot;docker-entrypoint.s…&quot;</span>   About an hour ago   Up About an hour             redis-node-3</span><br><span class="line">54a53ea6e7c9   redis:7.0.11   <span class="string">&quot;docker-entrypoint.s…&quot;</span>   About an hour ago   Up About an hour             redis-node-2</span><br><span class="line">14fcd03871a7   redis:7.0.11   <span class="string">&quot;docker-entrypoint.s…&quot;</span>   About an hour ago   Up 17 minutes                redis-node-1</span><br></pre></td></tr></table></figure>

<h4 id="进入-redis-node-7-容器实例"><a href="#进入-redis-node-7-容器实例" class="headerlink" title="进入 redis-node-7 容器实例"></a>进入 redis-node-7 容器实例</h4><figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">[root@hadoop100 redis]$ docker <span class="built_in">exec</span> -it redis-node-7 /bin/bash</span><br><span class="line">root@hadoop100:/data<span class="comment"># </span></span><br></pre></td></tr></table></figure>

<h4 id="将-redis-node-7-作为-master-加入集群"><a href="#将-redis-node-7-作为-master-加入集群" class="headerlink" title="将 redis-node-7 作为 master 加入集群"></a>将 redis-node-7 作为 master 加入集群</h4><figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">redis-cli --cluster add-node 添加节点ip:端口号 集群master的ip:端口号</span><br></pre></td></tr></table></figure>



<figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line">root@hadoop100:/data<span class="comment"># redis-cli --cluster add-node 192.168.8.100:6387 192.168.8.100:6381</span></span><br><span class="line">&gt;&gt;&gt; Adding node 192.168.8.100:6387 to cluster 192.168.8.100:6381</span><br><span class="line">&gt;&gt;&gt; Performing Cluster Check (using node 192.168.8.100:6381)</span><br><span class="line">M: 58a83b9597424d416edd7238d4a7ef7b88cba1d8 192.168.8.100:6381</span><br><span class="line">   slots:[0-5460] (5461 slots) master</span><br><span class="line">   1 additional replica(s)</span><br><span class="line">S: 85a2543184a999a01113c93de0614e3cfa40f13f 192.168.8.100:6384</span><br><span class="line">   slots: (0 slots) slave</span><br><span class="line">   replicates 5cd3f97b475d86589d8753dbc3d68879b9506d06</span><br><span class="line">S: 7829c3a9db5d9d7e8c01946bd5498a36e1a50e9e 192.168.8.100:6385</span><br><span class="line">   slots: (0 slots) slave</span><br><span class="line">   replicates 032776c9283be663a09dc31e101c84cca9972fb3</span><br><span class="line">M: 5cd3f97b475d86589d8753dbc3d68879b9506d06 192.168.8.100:6382</span><br><span class="line">   slots:[5461-10922] (5462 slots) master</span><br><span class="line">   1 additional replica(s)</span><br><span class="line">M: 032776c9283be663a09dc31e101c84cca9972fb3 192.168.8.100:6383</span><br><span class="line">   slots:[10923-16383] (5461 slots) master</span><br><span class="line">   1 additional replica(s)</span><br><span class="line">S: 4d65747f21c6fc491eda4c2af22be3343902577a 192.168.8.100:6386</span><br><span class="line">   slots: (0 slots) slave</span><br><span class="line">   replicates 58a83b9597424d416edd7238d4a7ef7b88cba1d8</span><br><span class="line">[OK] All nodes agree about slots configuration.</span><br><span class="line">&gt;&gt;&gt; Check <span class="keyword">for</span> open slots...</span><br><span class="line">&gt;&gt;&gt; Check slots coverage...</span><br><span class="line">[OK] All 16384 slots covered.</span><br><span class="line">&gt;&gt;&gt; Send CLUSTER MEET to node 192.168.8.100:6387 to make it <span class="built_in">join</span> the cluster.</span><br><span class="line">[OK] New node added correctly.</span><br></pre></td></tr></table></figure>

<h4 id="查看集群信息-1"><a href="#查看集群信息-1" class="headerlink" title="查看集群信息"></a>查看集群信息</h4><figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line">root@hadoop100:/data<span class="comment"># redis-cli --cluster check 192.168.8.100:6381</span></span><br><span class="line">192.168.8.100:6381 (58a83b95...) -&gt; 2 keys | 5461 slots | 1 slaves.</span><br><span class="line">192.168.8.100:6387 (2ff9c2fa...) -&gt; 0 keys | 0 slots | 0 slaves.					<span class="comment">#注意：这里6387已添加到集群，但是并没有被分配槽位和从机</span></span><br><span class="line">192.168.8.100:6382 (5cd3f97b...) -&gt; 1 keys | 5462 slots | 1 slaves.</span><br><span class="line">192.168.8.100:6383 (032776c9...) -&gt; 1 keys | 5461 slots | 1 slaves.</span><br><span class="line">[OK] 4 keys <span class="keyword">in</span> 4 masters.</span><br><span class="line">0.00 keys per slot on average.</span><br><span class="line">&gt;&gt;&gt; Performing Cluster Check (using node 192.168.8.100:6381)</span><br><span class="line">M: 58a83b9597424d416edd7238d4a7ef7b88cba1d8 192.168.8.100:6381</span><br><span class="line">   slots:[0-5460] (5461 slots) master</span><br><span class="line">   1 additional replica(s)</span><br><span class="line">S: 85a2543184a999a01113c93de0614e3cfa40f13f 192.168.8.100:6384</span><br><span class="line">   slots: (0 slots) slave</span><br><span class="line">   replicates 5cd3f97b475d86589d8753dbc3d68879b9506d06</span><br><span class="line">M: 2ff9c2fa9954cc9a5b1b82631613d8f628f9612c 192.168.8.100:6387</span><br><span class="line">   slots: (0 slots) master</span><br><span class="line">S: 7829c3a9db5d9d7e8c01946bd5498a36e1a50e9e 192.168.8.100:6385</span><br><span class="line">   slots: (0 slots) slave</span><br><span class="line">   replicates 032776c9283be663a09dc31e101c84cca9972fb3</span><br><span class="line">M: 5cd3f97b475d86589d8753dbc3d68879b9506d06 192.168.8.100:6382</span><br><span class="line">   slots:[5461-10922] (5462 slots) master</span><br><span class="line">   1 additional replica(s)</span><br><span class="line">M: 032776c9283be663a09dc31e101c84cca9972fb3 192.168.8.100:6383</span><br><span class="line">   slots:[10923-16383] (5461 slots) master</span><br><span class="line">   1 additional replica(s)</span><br><span class="line">S: 4d65747f21c6fc491eda4c2af22be3343902577a 192.168.8.100:6386</span><br><span class="line">   slots: (0 slots) slave</span><br><span class="line">   replicates 58a83b9597424d416edd7238d4a7ef7b88cba1d8</span><br><span class="line">[OK] All nodes agree about slots configuration.</span><br><span class="line">&gt;&gt;&gt; Check <span class="keyword">for</span> open slots...</span><br><span class="line">&gt;&gt;&gt; Check slots coverage...</span><br><span class="line">[OK] All 16384 slots covered.</span><br></pre></td></tr></table></figure>

<p>发现多了一个 master 节点，并且暂时是没有 slot 和 slaves</p>
<h4 id="重新分配-slot"><a href="#重新分配-slot" class="headerlink" title="重新分配 slot"></a>重新分配 slot</h4><figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">redis-cli --cluster reshard 192.168.8.100:6381</span><br></pre></td></tr></table></figure>

<p><img src="/oct25-xxxxx/img/docker/docker31.png"></p>
<h4 id="查看集群信息-2"><a href="#查看集群信息-2" class="headerlink" title="查看集群信息"></a>查看集群信息</h4><figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line">root@hadoop100:/data<span class="comment"># redis-cli --cluster check 192.168.8.100:6381</span></span><br><span class="line">192.168.8.100:6381 (58a83b95...) -&gt; 1 keys | 4096 slots | 1 slaves.</span><br><span class="line">192.168.8.100:6387 (2ff9c2fa...) -&gt; 1 keys | 4096 slots | 0 slaves.					<span class="comment">#已分配槽位</span></span><br><span class="line">192.168.8.100:6382 (5cd3f97b...) -&gt; 1 keys | 4096 slots | 1 slaves.</span><br><span class="line">192.168.8.100:6383 (032776c9...) -&gt; 1 keys | 4096 slots | 1 slaves.</span><br><span class="line">[OK] 4 keys <span class="keyword">in</span> 4 masters.</span><br><span class="line">0.00 keys per slot on average.</span><br><span class="line">&gt;&gt;&gt; Performing Cluster Check (using node 192.168.8.100:6381)</span><br><span class="line">M: 58a83b9597424d416edd7238d4a7ef7b88cba1d8 192.168.8.100:6381</span><br><span class="line">   slots:[1365-5460] (4096 slots) master</span><br><span class="line">   1 additional replica(s)</span><br><span class="line">S: 85a2543184a999a01113c93de0614e3cfa40f13f 192.168.8.100:6384</span><br><span class="line">   slots: (0 slots) slave</span><br><span class="line">   replicates 5cd3f97b475d86589d8753dbc3d68879b9506d06</span><br><span class="line">M: 2ff9c2fa9954cc9a5b1b82631613d8f628f9612c 192.168.8.100:6387</span><br><span class="line">   slots:[0-1364],[5461-6826],[10923-12287] (4096 slots) master						<span class="comment">#这里可以看到，并非全部槽位重新分配，而是每个槽位都匀出一部分槽位给到新节点</span></span><br><span class="line">S: 7829c3a9db5d9d7e8c01946bd5498a36e1a50e9e 192.168.8.100:6385</span><br><span class="line">   slots: (0 slots) slave</span><br><span class="line">   replicates 032776c9283be663a09dc31e101c84cca9972fb3</span><br><span class="line">M: 5cd3f97b475d86589d8753dbc3d68879b9506d06 192.168.8.100:6382</span><br><span class="line">   slots:[6827-10922] (4096 slots) master</span><br><span class="line">   1 additional replica(s)</span><br><span class="line">M: 032776c9283be663a09dc31e101c84cca9972fb3 192.168.8.100:6383</span><br><span class="line">   slots:[12288-16383] (4096 slots) master</span><br><span class="line">   1 additional replica(s)</span><br><span class="line">S: 4d65747f21c6fc491eda4c2af22be3343902577a 192.168.8.100:6386</span><br><span class="line">   slots: (0 slots) slave</span><br><span class="line">   replicates 58a83b9597424d416edd7238d4a7ef7b88cba1d8</span><br><span class="line">[OK] All nodes agree about slots configuration.</span><br><span class="line">&gt;&gt;&gt; Check <span class="keyword">for</span> open slots...</span><br><span class="line">&gt;&gt;&gt; Check slots coverage...</span><br><span class="line">[OK] All 16384 slots covered.	</span><br></pre></td></tr></table></figure>

<p>发现新的 slot 分配如下</p>
<table>
<thead>
<tr>
<th>master 节点</th>
<th>slot</th>
</tr>
</thead>
<tbody><tr>
<td>redis-node-1</td>
<td>[1365-5460]</td>
</tr>
<tr>
<td>redis-node-2</td>
<td>[6827-10922]</td>
</tr>
<tr>
<td>redis-node-3</td>
<td>[12288-16383]</td>
</tr>
<tr>
<td>redis-node-7</td>
<td>[0-1364],[5461-6826],[10923-12287]</td>
</tr>
</tbody></table>
<p>从集群当前信息发现，并不是完全重新将 16384 个slot 重新分配，而且每个 slot 匀给新节点一部分的 slot</p>
<p>为什么这样分配呢？完全重新分配，会涉及到已存在的数据，成本太高</p>
<h4 id="分配从节点"><a href="#分配从节点" class="headerlink" title="分配从节点"></a>分配从节点</h4><figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">redis-cli --cluster add-node 添加从节点IP:端口号 挂载master节点IP:端口号 --cluster-slave --cluster-master-id master节点的ID</span><br></pre></td></tr></table></figure>



<figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">redis-cli --cluster add-node 192.168.8.100:6388 192.168.8.100:6387 --cluster-slave --cluster-master-id 2ff9c2fa9954cc9a5b1b82631613d8f628f9612c</span><br></pre></td></tr></table></figure>

<h4 id="查看集群信息-3"><a href="#查看集群信息-3" class="headerlink" title="查看集群信息"></a>查看集群信息</h4><figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><span class="line">root@hadoop100:/data<span class="comment"># redis-cli --cluster check 192.168.8.100:6381</span></span><br><span class="line">192.168.8.100:6381 (58a83b95...) -&gt; 1 keys | 4096 slots | 1 slaves.</span><br><span class="line">192.168.8.100:6387 (2ff9c2fa...) -&gt; 1 keys | 4096 slots | 1 slaves.				<span class="comment">#添加了一个从节点</span></span><br><span class="line">192.168.8.100:6382 (5cd3f97b...) -&gt; 1 keys | 4096 slots | 1 slaves.</span><br><span class="line">192.168.8.100:6383 (032776c9...) -&gt; 1 keys | 4096 slots | 1 slaves.</span><br><span class="line">[OK] 4 keys <span class="keyword">in</span> 4 masters.</span><br><span class="line">0.00 keys per slot on average.</span><br><span class="line">&gt;&gt;&gt; Performing Cluster Check (using node 192.168.8.100:6381)</span><br><span class="line">M: 58a83b9597424d416edd7238d4a7ef7b88cba1d8 192.168.8.100:6381</span><br><span class="line">   slots:[1365-5460] (4096 slots) master</span><br><span class="line">   1 additional replica(s)</span><br><span class="line">S: 85a2543184a999a01113c93de0614e3cfa40f13f 192.168.8.100:6384</span><br><span class="line">   slots: (0 slots) slave</span><br><span class="line">   replicates 5cd3f97b475d86589d8753dbc3d68879b9506d06</span><br><span class="line">S: a9556e232fb5022568d29e6da2dfa70820da8b08 192.168.8.100:6388</span><br><span class="line">   slots: (0 slots) slave</span><br><span class="line">   replicates 2ff9c2fa9954cc9a5b1b82631613d8f628f9612c</span><br><span class="line">M: 2ff9c2fa9954cc9a5b1b82631613d8f628f9612c 192.168.8.100:6387</span><br><span class="line">   slots:[0-1364],[5461-6826],[10923-12287] (4096 slots) master</span><br><span class="line">   1 additional replica(s)</span><br><span class="line">S: 7829c3a9db5d9d7e8c01946bd5498a36e1a50e9e 192.168.8.100:6385</span><br><span class="line">   slots: (0 slots) slave</span><br><span class="line">   replicates 032776c9283be663a09dc31e101c84cca9972fb3</span><br><span class="line">M: 5cd3f97b475d86589d8753dbc3d68879b9506d06 192.168.8.100:6382</span><br><span class="line">   slots:[6827-10922] (4096 slots) master</span><br><span class="line">   1 additional replica(s)</span><br><span class="line">M: 032776c9283be663a09dc31e101c84cca9972fb3 192.168.8.100:6383</span><br><span class="line">   slots:[12288-16383] (4096 slots) master</span><br><span class="line">   1 additional replica(s)</span><br><span class="line">S: 4d65747f21c6fc491eda4c2af22be3343902577a 192.168.8.100:6386</span><br><span class="line">   slots: (0 slots) slave</span><br><span class="line">   replicates 58a83b9597424d416edd7238d4a7ef7b88cba1d8</span><br><span class="line">[OK] All nodes agree about slots configuration.</span><br><span class="line">&gt;&gt;&gt; Check <span class="keyword">for</span> open slots...</span><br><span class="line">&gt;&gt;&gt; Check slots coverage...</span><br><span class="line">[OK] All 16384 slots covered.</span><br></pre></td></tr></table></figure>

<p>可以看到 6387 节点多了一个从节点</p>
<h3 id="主从缩容"><a href="#主从缩容" class="headerlink" title="主从缩容"></a>主从缩容</h3><table>
<thead>
<tr>
<th>主机</th>
<th>端口</th>
<th>slot</th>
<th>从机</th>
<th>端口</th>
</tr>
</thead>
<tbody><tr>
<td>redis-node-1</td>
<td>6381</td>
<td>[1365-5460]</td>
<td>redis-node-6</td>
<td>6386</td>
</tr>
<tr>
<td>redis-node-2</td>
<td>6382</td>
<td>[6827-10922]</td>
<td>redis-node-4</td>
<td>6384</td>
</tr>
<tr>
<td>redis-node-3</td>
<td>6383</td>
<td>[12288-16383]</td>
<td>redis-node-5</td>
<td>6385</td>
</tr>
<tr>
<td>redis-node-7</td>
<td>6387</td>
<td>[0-1364],[5461-6826],[10923-12287]</td>
<td>redis-node-8</td>
<td>6388</td>
</tr>
</tbody></table>
<p>缩容时，还是会涉及到 slot 的重新分配，思考？</p>
<ul>
<li>是如何重新分配，slot 打乱重洗，还是原路返回？</li>
<li>先删slave，还是 master？ &#x3D;&#x3D;》先删除 slave</li>
</ul>
<h4 id="将-redis-node-8-删除"><a href="#将-redis-node-8-删除" class="headerlink" title="将 redis-node-8 删除"></a>将 redis-node-8 删除</h4><figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">redis-cli --cluster del-node 要删除slot的ip:端口号 要删除slot的<span class="built_in">id</span></span><br></pre></td></tr></table></figure>



<figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">root@hadoop100:/data<span class="comment"># redis-cli --cluster del-node 192.168.8.100:6388 a9556e232fb5022568d29e6da2dfa70820da8b08</span></span><br><span class="line">&gt;&gt;&gt; Removing node a9556e232fb5022568d29e6da2dfa70820da8b08 from cluster 192.168.8.100:6388</span><br><span class="line">&gt;&gt;&gt; Sending CLUSTER FORGET messages to the cluster...</span><br><span class="line">&gt;&gt;&gt; Sending CLUSTER RESET SOFT to the deleted node.</span><br></pre></td></tr></table></figure>

<h4 id="查看集群信息-4"><a href="#查看集群信息-4" class="headerlink" title="查看集群信息"></a>查看集群信息</h4><p>查看从节点是否被删除</p>
<figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line">![docker32](D:\其他\笔记资料\BigData19_开源应用服务引擎Docker\img\docker32.png)root@hadoop100:/data<span class="comment"># redis-cli --cluster check 192.168.8.100:6381</span></span><br><span class="line">192.168.8.100:6381 (58a83b95...) -&gt; 1 keys | 4096 slots | 1 slaves.</span><br><span class="line">192.168.8.100:6387 (2ff9c2fa...) -&gt; 1 keys | 4096 slots | 0 slaves.</span><br><span class="line">192.168.8.100:6382 (5cd3f97b...) -&gt; 1 keys | 4096 slots | 1 slaves.</span><br><span class="line">192.168.8.100:6383 (032776c9...) -&gt; 1 keys | 4096 slots | 1 slaves.</span><br><span class="line">[OK] 4 keys <span class="keyword">in</span> 4 masters.</span><br><span class="line">0.00 keys per slot on average.</span><br><span class="line">&gt;&gt;&gt; Performing Cluster Check (using node 192.168.8.100:6381)</span><br><span class="line">M: 58a83b9597424d416edd7238d4a7ef7b88cba1d8 192.168.8.100:6381</span><br><span class="line">   slots:[1365-5460] (4096 slots) master</span><br><span class="line">   1 additional replica(s)</span><br><span class="line">S: 85a2543184a999a01113c93de0614e3cfa40f13f 192.168.8.100:6384</span><br><span class="line">   slots: (0 slots) slave</span><br><span class="line">   replicates 5cd3f97b475d86589d8753dbc3d68879b9506d06</span><br><span class="line">M: 2ff9c2fa9954cc9a5b1b82631613d8f628f9612c 192.168.8.100:6387</span><br><span class="line">   slots:[0-1364],[5461-6826],[10923-12287] (4096 slots) master</span><br><span class="line">S: 7829c3a9db5d9d7e8c01946bd5498a36e1a50e9e 192.168.8.100:6385</span><br><span class="line">   slots: (0 slots) slave</span><br><span class="line">   replicates 032776c9283be663a09dc31e101c84cca9972fb3</span><br><span class="line">M: 5cd3f97b475d86589d8753dbc3d68879b9506d06 192.168.8.100:6382</span><br><span class="line">   slots:[6827-10922] (4096 slots) master</span><br><span class="line">   1 additional replica(s)</span><br><span class="line">M: 032776c9283be663a09dc31e101c84cca9972fb3 192.168.8.100:6383</span><br><span class="line">   slots:[12288-16383] (4096 slots) master</span><br><span class="line">   1 additional replica(s)</span><br><span class="line">S: 4d65747f21c6fc491eda4c2af22be3343902577a 192.168.8.100:6386</span><br><span class="line">   slots: (0 slots) slave</span><br><span class="line">   replicates 58a83b9597424d416edd7238d4a7ef7b88cba1d8</span><br><span class="line">[OK] All nodes agree about slots configuration.</span><br><span class="line">&gt;&gt;&gt; Check <span class="keyword">for</span> open slots...</span><br><span class="line">&gt;&gt;&gt; Check slots coverage...</span><br><span class="line">[OK] All 16384 slots covered.</span><br></pre></td></tr></table></figure>

<h4 id="重新分配-slot-1"><a href="#重新分配-slot-1" class="headerlink" title="重新分配 slot"></a>重新分配 slot</h4><p>将 redis-node-7 的 slot 清空，重新分配</p>
<figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">redis-cli --cluster reshard 192.168.8.100:6381</span><br></pre></td></tr></table></figure>

<p><img src="/oct25-xxxxx/img/docker/docker32.png"></p>
<p>注意：这里的 slot 源可以是多个，就是说 4096 个 slot 可以由多个节点组成</p>
<h4 id="查看集群信息-5"><a href="#查看集群信息-5" class="headerlink" title="查看集群信息"></a>查看集群信息</h4><figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line">root@hadoop100:/data<span class="comment"># redis-cli --cluster check 192.168.8.100:6381</span></span><br><span class="line">192.168.8.100:6381 (58a83b95...) -&gt; 2 keys | 8192 slots | 1 slaves.</span><br><span class="line">192.168.8.100:6387 (2ff9c2fa...) -&gt; 0 keys | 0 slots | 0 slaves.</span><br><span class="line">192.168.8.100:6382 (5cd3f97b...) -&gt; 1 keys | 4096 slots | 1 slaves.</span><br><span class="line">192.168.8.100:6383 (032776c9...) -&gt; 1 keys | 4096 slots | 1 slaves.</span><br><span class="line">[OK] 4 keys <span class="keyword">in</span> 4 masters.</span><br><span class="line">0.00 keys per slot on average.</span><br><span class="line">&gt;&gt;&gt; Performing Cluster Check (using node 192.168.8.100:6381)</span><br><span class="line">M: 58a83b9597424d416edd7238d4a7ef7b88cba1d8 192.168.8.100:6381</span><br><span class="line">   slots:[0-6826],[10923-12287] (8192 slots) master</span><br><span class="line">   1 additional replica(s)</span><br><span class="line">S: 85a2543184a999a01113c93de0614e3cfa40f13f 192.168.8.100:6384</span><br><span class="line">   slots: (0 slots) slave</span><br><span class="line">   replicates 5cd3f97b475d86589d8753dbc3d68879b9506d06</span><br><span class="line">M: 2ff9c2fa9954cc9a5b1b82631613d8f628f9612c 192.168.8.100:6387</span><br><span class="line">   slots: (0 slots) master</span><br><span class="line">S: 7829c3a9db5d9d7e8c01946bd5498a36e1a50e9e 192.168.8.100:6385</span><br><span class="line">   slots: (0 slots) slave</span><br><span class="line">   replicates 032776c9283be663a09dc31e101c84cca9972fb3</span><br><span class="line">M: 5cd3f97b475d86589d8753dbc3d68879b9506d06 192.168.8.100:6382</span><br><span class="line">   slots:[6827-10922] (4096 slots) master</span><br><span class="line">   1 additional replica(s)</span><br><span class="line">M: 032776c9283be663a09dc31e101c84cca9972fb3 192.168.8.100:6383</span><br><span class="line">   slots:[12288-16383] (4096 slots) master</span><br><span class="line">   1 additional replica(s)</span><br><span class="line">S: 4d65747f21c6fc491eda4c2af22be3343902577a 192.168.8.100:6386</span><br><span class="line">   slots: (0 slots) slave</span><br><span class="line">   replicates 58a83b9597424d416edd7238d4a7ef7b88cba1d8</span><br><span class="line">[OK] All nodes agree about slots configuration.</span><br><span class="line">&gt;&gt;&gt; Check <span class="keyword">for</span> open slots...</span><br><span class="line">&gt;&gt;&gt; Check slots coverage...</span><br><span class="line">[OK] All 16384 slots covered.</span><br></pre></td></tr></table></figure>

<p>当前 redis-node-7 节点上没有 slot 也没有 slave 了</p>
<h4 id="删除节点"><a href="#删除节点" class="headerlink" title="删除节点"></a>删除节点</h4><figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">redis-cli --cluster del-node 删除节点ip:端口号 删除节点<span class="built_in">id</span></span><br></pre></td></tr></table></figure>



<figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">root@hadoop100:/data<span class="comment"># redis-cli --cluster del-node 192.168.8.100:6387 2ff9c2fa9954cc9a5b1b82631613d8f628f9612c</span></span><br><span class="line">&gt;&gt;&gt; Removing node 2ff9c2fa9954cc9a5b1b82631613d8f628f9612c from cluster 192.168.8.100:6387</span><br><span class="line">&gt;&gt;&gt; Sending CLUSTER FORGET messages to the cluster...</span><br><span class="line">&gt;&gt;&gt; Sending CLUSTER RESET SOFT to the deleted node.</span><br></pre></td></tr></table></figure>

<h4 id="查看集群信息-6"><a href="#查看集群信息-6" class="headerlink" title="查看集群信息"></a>查看集群信息</h4><figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line">root@hadoop100:/data<span class="comment"># redis-cli --cluster check 192.168.8.100:6381</span></span><br><span class="line">192.168.8.100:6381 (58a83b95...) -&gt; 2 keys | 8192 slots | 1 slaves.</span><br><span class="line">192.168.8.100:6382 (5cd3f97b...) -&gt; 1 keys | 4096 slots | 1 slaves.</span><br><span class="line">192.168.8.100:6383 (032776c9...) -&gt; 1 keys | 4096 slots | 1 slaves.</span><br><span class="line">[OK] 4 keys <span class="keyword">in</span> 3 masters.</span><br><span class="line">0.00 keys per slot on average.</span><br><span class="line">&gt;&gt;&gt; Performing Cluster Check (using node 192.168.8.100:6381)</span><br><span class="line">M: 58a83b9597424d416edd7238d4a7ef7b88cba1d8 192.168.8.100:6381</span><br><span class="line">   slots:[0-6826],[10923-12287] (8192 slots) master</span><br><span class="line">   1 additional replica(s)</span><br><span class="line">S: 85a2543184a999a01113c93de0614e3cfa40f13f 192.168.8.100:6384</span><br><span class="line">   slots: (0 slots) slave</span><br><span class="line">   replicates 5cd3f97b475d86589d8753dbc3d68879b9506d06</span><br><span class="line">S: 7829c3a9db5d9d7e8c01946bd5498a36e1a50e9e 192.168.8.100:6385</span><br><span class="line">   slots: (0 slots) slave</span><br><span class="line">   replicates 032776c9283be663a09dc31e101c84cca9972fb3</span><br><span class="line">M: 5cd3f97b475d86589d8753dbc3d68879b9506d06 192.168.8.100:6382</span><br><span class="line">   slots:[6827-10922] (4096 slots) master</span><br><span class="line">   1 additional replica(s)</span><br><span class="line">M: 032776c9283be663a09dc31e101c84cca9972fb3 192.168.8.100:6383</span><br><span class="line">   slots:[12288-16383] (4096 slots) master</span><br><span class="line">   1 additional replica(s)</span><br><span class="line">S: 4d65747f21c6fc491eda4c2af22be3343902577a 192.168.8.100:6386</span><br><span class="line">   slots: (0 slots) slave</span><br><span class="line">   replicates 58a83b9597424d416edd7238d4a7ef7b88cba1d8</span><br><span class="line">[OK] All nodes agree about slots configuration.</span><br><span class="line">&gt;&gt;&gt; Check <span class="keyword">for</span> open slots...</span><br><span class="line">&gt;&gt;&gt; Check slots coverage...</span><br><span class="line">[OK] All 16384 slots covered.</span><br></pre></td></tr></table></figure>























































</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="mailto:undefined">Oct25-X</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="https://oct25-x.atomgit.net/oct25-xxxxx/2023/11/22/37%E5%BC%80%E6%BA%90%E5%BA%94%E7%94%A8%E5%AE%B9%E5%99%A8%E5%BC%95%E6%93%8EDocker_03%E5%B8%B8%E7%94%A8%E7%BB%84%E4%BB%B6Docker%E5%AE%89%E8%A3%85/">https://oct25-x.atomgit.net/oct25-xxxxx/2023/11/22/37%E5%BC%80%E6%BA%90%E5%BA%94%E7%94%A8%E5%AE%B9%E5%99%A8%E5%BC%95%E6%93%8EDocker_03%E5%B8%B8%E7%94%A8%E7%BB%84%E4%BB%B6Docker%E5%AE%89%E8%A3%85/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="https://oct25-x.atomgit.net/oct25-xxxxx" target="_blank">Oct25-X</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/oct25-xxxxx/tags/Docker/">Docker</a></div><div class="post_share"><div class="social-share" data-image="/oct25-xxxxx/img/cover_img/ctt37.png" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/social-share.js/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/social-share.js/dist/js/social-share.min.js" defer></script></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/oct25-xxxxx/2023/12/06/38%E5%BC%80%E6%BA%90%E5%BA%94%E7%94%A8%E5%AE%B9%E5%99%A8%E5%BC%95%E6%93%8EDocker_04Dockerfile/"><img class="prev-cover" src="/oct25-xxxxx/img/cover_img/ctt38.png" onerror="onerror=null;src='/oct25-xxxxx/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">开源应用容器引擎 Docker_04Dockerfile</div></div></a></div><div class="next-post pull-right"><a href="/oct25-xxxxx/2023/11/16/36%E5%BC%80%E6%BA%90%E5%BA%94%E7%94%A8%E5%AE%B9%E5%99%A8%E5%BC%95%E6%93%8EDocker_02Docker%E9%95%9C%E5%83%8F%E5%92%8C%E5%AE%B9%E5%99%A8%E6%95%B0%E6%8D%AE%E5%8D%B7/"><img class="next-cover" src="/oct25-xxxxx/img/cover_img/ctt36.png" onerror="onerror=null;src='/oct25-xxxxx/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">开源应用容器引擎 Docker_02Docker镜像和容器数据卷</div></div></a></div></nav><div class="relatedPosts"><div class="headline"><i class="fas fa-thumbs-up fa-fw"></i><span>相关推荐</span></div><div class="relatedPosts-list"><div><a href="/oct25-xxxxx/2023/11/03/35%E5%BC%80%E6%BA%90%E5%BA%94%E7%94%A8%E5%AE%B9%E5%99%A8%E5%BC%95%E6%93%8EDocker_01Docker%E5%85%A5%E9%97%A8/" title="开源应用容器引擎 Docker_01Docker入门"><img class="cover" src="/oct25-xxxxx/img/cover_img/ctt35.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2023-11-03</div><div class="title">开源应用容器引擎 Docker_01Docker入门</div></div></a></div><div><a href="/oct25-xxxxx/2023/11/16/36%E5%BC%80%E6%BA%90%E5%BA%94%E7%94%A8%E5%AE%B9%E5%99%A8%E5%BC%95%E6%93%8EDocker_02Docker%E9%95%9C%E5%83%8F%E5%92%8C%E5%AE%B9%E5%99%A8%E6%95%B0%E6%8D%AE%E5%8D%B7/" title="开源应用容器引擎 Docker_02Docker镜像和容器数据卷"><img class="cover" src="/oct25-xxxxx/img/cover_img/ctt36.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2023-11-16</div><div class="title">开源应用容器引擎 Docker_02Docker镜像和容器数据卷</div></div></a></div><div><a href="/oct25-xxxxx/2023/12/06/38%E5%BC%80%E6%BA%90%E5%BA%94%E7%94%A8%E5%AE%B9%E5%99%A8%E5%BC%95%E6%93%8EDocker_04Dockerfile/" title="开源应用容器引擎 Docker_04Dockerfile"><img class="cover" src="/oct25-xxxxx/img/cover_img/ctt38.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2023-12-06</div><div class="title">开源应用容器引擎 Docker_04Dockerfile</div></div></a></div><div><a href="/oct25-xxxxx/2023/12/19/39%E5%BC%80%E6%BA%90%E5%BA%94%E7%94%A8%E5%AE%B9%E5%99%A8%E5%BC%95%E6%93%8EDocker_05Docker%E7%BD%91%E7%BB%9C/" title="开源应用容器引擎 Docker_05Docker网络和Comprose容器编排"><img class="cover" src="/oct25-xxxxx/img/cover_img/ctt39.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2023-12-19</div><div class="title">开源应用容器引擎 Docker_05Docker网络和Comprose容器编排</div></div></a></div><div><a href="/oct25-xxxxx/2023/12/26/40%E5%BC%80%E6%BA%90%E5%BA%94%E7%94%A8%E5%AE%B9%E5%99%A8%E5%BC%95%E6%93%8EDocker_06%E8%BD%BB%E9%87%8F%E7%BA%A7%E5%8F%AF%E8%A7%86%E5%8C%96%E5%B7%A5%E5%85%B7Portainer/" title="开源应用容器引擎 Docker_06轻量级可视化工具Portainer"><img class="cover" src="/oct25-xxxxx/img/cover_img/ctt40.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2023-12-26</div><div class="title">开源应用容器引擎 Docker_06轻量级可视化工具Portainer</div></div></a></div></div></div></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src="/oct25-xxxxx/img/avatar.jpg" onerror="this.onerror=null;this.src='/oct25-xxxxx/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">Oct25-X</div><div class="author-info__description"></div></div><div class="card-info-data is-center"><div class="card-info-data-item"><a href="/oct25-xxxxx/archives/"><div class="headline">文章</div><div class="length-num">52</div></a></div><div class="card-info-data-item"><a href="/oct25-xxxxx/tags/"><div class="headline">标签</div><div class="length-num">17</div></a></div><div class="card-info-data-item"><a href="/oct25-xxxxx/categories/"><div class="headline">分类</div><div class="length-num">8</div></a></div></div><a id="card-info-btn" target="_blank" rel="noopener" href="https://github.com/xxxxxx"><i class="fab fa-github"></i><span>Follow Me</span></a></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span><span class="toc-percentage"></span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#Docer-%E5%AE%89%E8%A3%85%E6%95%B4%E4%BD%93%E6%AD%A5%E9%AA%A4"><span class="toc-number">1.</span> <span class="toc-text">Docer 安装整体步骤</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E5%8D%95%E6%9C%BA%E9%83%A8%E7%BD%B2-MySQL-x2F-Redis"><span class="toc-number">2.</span> <span class="toc-text">单机部署 MySQL&#x2F;Redis</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%B8%80%E3%80%81%E5%AE%89%E8%A3%85-MySQL8"><span class="toc-number">2.1.</span> <span class="toc-text">一、安装 MySQL8</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%AE%89%E8%A3%85%E6%AD%A5%E9%AA%A4"><span class="toc-number">2.1.1.</span> <span class="toc-text">安装步骤</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%87%BA%E7%8E%B0%E7%9A%84%E9%97%AE%E9%A2%98"><span class="toc-number">2.1.2.</span> <span class="toc-text">出现的问题</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%AE%9E%E9%99%85%E9%83%A8%E7%BD%B2%E6%AD%A5%E9%AA%A4"><span class="toc-number">2.1.3.</span> <span class="toc-text">实际部署步骤</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%80%9D%E8%80%83"><span class="toc-number">2.1.4.</span> <span class="toc-text">思考</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%B8%80%E3%80%81%E5%AE%89%E8%A3%85-Redis"><span class="toc-number">2.2.</span> <span class="toc-text">一、安装 Redis</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%AE%89%E8%A3%85-redis-7-0-11"><span class="toc-number">2.2.1.</span> <span class="toc-text">安装 redis-7.0.11</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%AE%9E%E9%99%85%E9%83%A8%E7%BD%B2%E6%AD%A5%E9%AA%A4-1"><span class="toc-number">2.2.2.</span> <span class="toc-text">实际部署步骤</span></a></li></ol></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E9%9B%86%E7%BE%A4%E9%83%A8%E7%BD%B2-MySQL-x2F-Redis"><span class="toc-number">3.</span> <span class="toc-text">集群部署 MySQL&#x2F;Redis</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%B8%80%E3%80%81MySQL-%E4%B8%BB%E4%BB%8E%E5%A4%8D%E5%88%B6%E9%83%A8%E7%BD%B2"><span class="toc-number">3.1.</span> <span class="toc-text">一、MySQL 主从复制部署</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E8%A7%92%E8%89%B2%E8%A7%84%E5%88%92"><span class="toc-number">3.1.1.</span> <span class="toc-text">角色规划</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#mysql8-3307%EF%BC%88master%EF%BC%89"><span class="toc-number">3.1.2.</span> <span class="toc-text">mysql8-3307（master）</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#mysql8-3308%EF%BC%88slave%EF%BC%89"><span class="toc-number">3.1.3.</span> <span class="toc-text">mysql8-3308（slave）</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%9C%A8-master-%E6%9F%A5%E7%9C%8B%E7%8A%B6%E6%80%81%EF%BC%88mysql8-3307%EF%BC%89"><span class="toc-number">3.1.4.</span> <span class="toc-text">在 master 查看状态（mysql8-3307）</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%9C%A8-slave-%E4%B8%AD%E9%85%8D%E7%BD%AE%E4%B8%BB%E4%BB%8E%E5%A4%8D%E5%88%B6%EF%BC%88mysql8-3308%EF%BC%89"><span class="toc-number">3.1.5.</span> <span class="toc-text">在 slave 中配置主从复制（mysql8-3308）</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%9C%A8-slave-%E4%B8%AD%E6%9F%A5%E7%9C%8B%E4%B8%BB%E4%BB%8E%E5%90%8C%E6%AD%A5%E7%8A%B6%E6%80%81%EF%BC%88mysql8-3308%EF%BC%89"><span class="toc-number">3.1.6.</span> <span class="toc-text">在 slave 中查看主从同步状态（mysql8-3308）</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%9C%A8-slave-%E4%B8%AD%E5%BC%80%E5%A7%8B%E4%B8%BB%E4%BB%8E%E5%A4%8D%E5%88%B6%EF%BC%88mysql8-3308%EF%BC%89"><span class="toc-number">3.1.7.</span> <span class="toc-text">在 slave 中开始主从复制（mysql8-3308）</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%9C%A8-slave-%E4%B8%AD%E5%86%8D%E6%AC%A1%E6%9F%A5%E7%9C%8B%E7%8A%B6%E6%80%81%EF%BC%88mysql8-3308%EF%BC%89"><span class="toc-number">3.1.8.</span> <span class="toc-text">在 slave 中再次查看状态（mysql8-3308）</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%B8%BB%E4%BB%8E%E5%A4%8D%E5%88%B6%E6%B5%8B%E8%AF%95"><span class="toc-number">3.1.9.</span> <span class="toc-text">主从复制测试</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E9%97%AE%E9%A2%98"><span class="toc-number">3.1.10.</span> <span class="toc-text">问题</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%BA%8C%E3%80%81Redis-%E9%9B%86%E7%BE%A4"><span class="toc-number">3.2.</span> <span class="toc-text">二、Redis 集群</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%88%86%E5%B8%83%E5%BC%8F%E8%AE%BE%E8%AE%A1%E6%96%B9%E6%A1%88"><span class="toc-number">3.2.1.</span> <span class="toc-text">分布式设计方案</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#Hash-%E5%8F%96%E4%BD%99%E5%88%86%E5%8C%BA"><span class="toc-number">3.2.1.1.</span> <span class="toc-text">Hash 取余分区</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E4%B8%80%E8%87%B4%E6%80%A7-Hash-%E7%AE%97%E6%B3%95"><span class="toc-number">3.2.1.2.</span> <span class="toc-text">一致性 Hash 算法</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#Hash-%E6%A7%BD%E5%88%86%E5%8C%BA"><span class="toc-number">3.2.1.3.</span> <span class="toc-text">Hash 槽分区</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%B8%89%E4%B8%BB%E4%B8%89%E4%BB%8E-Redis-%E9%9B%86%E7%BE%A4%E6%90%AD%E5%BB%BA"><span class="toc-number">3.2.2.</span> <span class="toc-text">三主三从 Redis 集群搭建</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E8%A7%92%E8%89%B2%E5%88%86%E9%85%8D"><span class="toc-number">3.2.2.1.</span> <span class="toc-text">角色分配</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%85%B3%E9%97%AD%E9%98%B2%E7%81%AB%E5%A2%99"><span class="toc-number">3.2.2.2.</span> <span class="toc-text">关闭防火墙</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%90%AF%E5%8A%A8docker%E5%90%8E%E5%8F%B0%E6%9C%8D%E5%8A%A1"><span class="toc-number">3.2.2.3.</span> <span class="toc-text">启动docker后台服务</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%96%B0%E5%BB%BA6%E4%B8%AAdocker%E5%AE%B9%E5%99%A8%E5%AE%9E%E4%BE%8B"><span class="toc-number">3.2.2.4.</span> <span class="toc-text">新建6个docker容器实例</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E8%BF%9B%E5%85%A5redis-node-1-%E5%AE%B9%E5%99%A8"><span class="toc-number">3.2.2.5.</span> <span class="toc-text">进入redis-node-1 容器</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%9E%84%E5%BB%BA%E4%B8%BB%E4%BB%8E%E5%85%B3%E7%B3%BB"><span class="toc-number">3.2.2.6.</span> <span class="toc-text">构建主从关系</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E8%BF%9E%E6%8E%A5%E9%9B%86%E7%BE%A4"><span class="toc-number">3.2.3.</span> <span class="toc-text">连接集群</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E8%BF%9E%E6%8E%A5%E4%BF%A1%E6%81%AF%E6%9F%A5%E7%9C%8B%E9%9B%86%E7%BE%A4%E4%BF%A1%E6%81%AF"><span class="toc-number">3.2.4.</span> <span class="toc-text">连接信息查看集群信息</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E8%BF%9B%E5%85%A56381%E6%9F%A5%E7%9C%8B%E9%9B%86%E7%BE%A4%E7%8A%B6%E6%80%81"><span class="toc-number">3.2.4.1.</span> <span class="toc-text">进入6381查看集群状态</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%9F%A5%E7%9C%8B%E8%8A%82%E7%82%B9%E4%BF%A1%E6%81%AF"><span class="toc-number">3.2.4.2.</span> <span class="toc-text">查看节点信息</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%9F%A5%E7%9C%8B%E9%9B%86%E7%BE%A4%E4%BF%A1%E6%81%AF"><span class="toc-number">3.2.4.3.</span> <span class="toc-text">查看集群信息</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%AE%B9%E9%94%99%E5%88%87%E6%8D%A2%E8%BF%81%E7%A7%BB"><span class="toc-number">3.2.5.</span> <span class="toc-text">容错切换迁移</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%81%9C%E6%AD%A2-redis-node-1"><span class="toc-number">3.2.5.1.</span> <span class="toc-text">停止 redis-node-1</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E9%87%8D%E6%96%B0%E9%87%8D%E5%90%AF-redis-node-1"><span class="toc-number">3.2.5.2.</span> <span class="toc-text">重新重启 redis-node-1</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%B8%BB%E4%BB%8E%E6%89%A9%E5%AE%B9"><span class="toc-number">3.2.6.</span> <span class="toc-text">主从扩容</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%90%AF%E5%8A%A8%E4%B8%A4%E4%B8%AA%E6%96%B0%E8%8A%82%E7%82%B9"><span class="toc-number">3.2.6.1.</span> <span class="toc-text">启动两个新节点</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E8%BF%9B%E5%85%A5-redis-node-7-%E5%AE%B9%E5%99%A8%E5%AE%9E%E4%BE%8B"><span class="toc-number">3.2.6.2.</span> <span class="toc-text">进入 redis-node-7 容器实例</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%B0%86-redis-node-7-%E4%BD%9C%E4%B8%BA-master-%E5%8A%A0%E5%85%A5%E9%9B%86%E7%BE%A4"><span class="toc-number">3.2.6.3.</span> <span class="toc-text">将 redis-node-7 作为 master 加入集群</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%9F%A5%E7%9C%8B%E9%9B%86%E7%BE%A4%E4%BF%A1%E6%81%AF-1"><span class="toc-number">3.2.6.4.</span> <span class="toc-text">查看集群信息</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E9%87%8D%E6%96%B0%E5%88%86%E9%85%8D-slot"><span class="toc-number">3.2.6.5.</span> <span class="toc-text">重新分配 slot</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%9F%A5%E7%9C%8B%E9%9B%86%E7%BE%A4%E4%BF%A1%E6%81%AF-2"><span class="toc-number">3.2.6.6.</span> <span class="toc-text">查看集群信息</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%88%86%E9%85%8D%E4%BB%8E%E8%8A%82%E7%82%B9"><span class="toc-number">3.2.6.7.</span> <span class="toc-text">分配从节点</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%9F%A5%E7%9C%8B%E9%9B%86%E7%BE%A4%E4%BF%A1%E6%81%AF-3"><span class="toc-number">3.2.6.8.</span> <span class="toc-text">查看集群信息</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%B8%BB%E4%BB%8E%E7%BC%A9%E5%AE%B9"><span class="toc-number">3.2.7.</span> <span class="toc-text">主从缩容</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%B0%86-redis-node-8-%E5%88%A0%E9%99%A4"><span class="toc-number">3.2.7.1.</span> <span class="toc-text">将 redis-node-8 删除</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%9F%A5%E7%9C%8B%E9%9B%86%E7%BE%A4%E4%BF%A1%E6%81%AF-4"><span class="toc-number">3.2.7.2.</span> <span class="toc-text">查看集群信息</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E9%87%8D%E6%96%B0%E5%88%86%E9%85%8D-slot-1"><span class="toc-number">3.2.7.3.</span> <span class="toc-text">重新分配 slot</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%9F%A5%E7%9C%8B%E9%9B%86%E7%BE%A4%E4%BF%A1%E6%81%AF-5"><span class="toc-number">3.2.7.4.</span> <span class="toc-text">查看集群信息</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%88%A0%E9%99%A4%E8%8A%82%E7%82%B9"><span class="toc-number">3.2.7.5.</span> <span class="toc-text">删除节点</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%9F%A5%E7%9C%8B%E9%9B%86%E7%BE%A4%E4%BF%A1%E6%81%AF-6"><span class="toc-number">3.2.7.6.</span> <span class="toc-text">查看集群信息</span></a></li></ol></li></ol></li></ol></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/oct25-xxxxx/2024/02/24/52%E5%A4%A7%E6%95%B0%E6%8D%AE%E4%B9%8B%E5%A4%9A%E8%8C%83%E5%BC%8F%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80Scala09_%E9%9A%90%E5%BC%8F%E8%BD%AC%E6%8D%A2%E3%80%81%E5%BC%82%E5%B8%B8%E5%92%8C%E6%B3%9B%E5%9E%8B/" title="大数据之多范式编程语言 Scala09_隐式转换、异常和泛型"><img src="/oct25-xxxxx/img/cover_img/ctt52.png" onerror="this.onerror=null;this.src='/oct25-xxxxx/img/404.jpg'" alt="大数据之多范式编程语言 Scala09_隐式转换、异常和泛型"/></a><div class="content"><a class="title" href="/oct25-xxxxx/2024/02/24/52%E5%A4%A7%E6%95%B0%E6%8D%AE%E4%B9%8B%E5%A4%9A%E8%8C%83%E5%BC%8F%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80Scala09_%E9%9A%90%E5%BC%8F%E8%BD%AC%E6%8D%A2%E3%80%81%E5%BC%82%E5%B8%B8%E5%92%8C%E6%B3%9B%E5%9E%8B/" title="大数据之多范式编程语言 Scala09_隐式转换、异常和泛型">大数据之多范式编程语言 Scala09_隐式转换、异常和泛型</a><time datetime="2024-02-24T04:55:34.000Z" title="发表于 2024-02-24 12:55:34">2024-02-24</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/oct25-xxxxx/2024/02/24/51%E5%A4%A7%E6%95%B0%E6%8D%AE%E4%B9%8B%E5%A4%9A%E8%8C%83%E5%BC%8F%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80Scala08_%E6%A8%A1%E5%BC%8F%E5%8C%B9%E9%85%8D/" title="大数据之多范式编程语言 Scala08_模式匹配"><img src="/oct25-xxxxx/img/cover_img/ctt51.png" onerror="this.onerror=null;this.src='/oct25-xxxxx/img/404.jpg'" alt="大数据之多范式编程语言 Scala08_模式匹配"/></a><div class="content"><a class="title" href="/oct25-xxxxx/2024/02/24/51%E5%A4%A7%E6%95%B0%E6%8D%AE%E4%B9%8B%E5%A4%9A%E8%8C%83%E5%BC%8F%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80Scala08_%E6%A8%A1%E5%BC%8F%E5%8C%B9%E9%85%8D/" title="大数据之多范式编程语言 Scala08_模式匹配">大数据之多范式编程语言 Scala08_模式匹配</a><time datetime="2024-02-24T00:46:12.000Z" title="发表于 2024-02-24 08:46:12">2024-02-24</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/oct25-xxxxx/2024/02/22/50%E5%A4%A7%E6%95%B0%E6%8D%AE%E4%B9%8B%E5%A4%9A%E8%8C%83%E5%BC%8F%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80Scala07_%E9%9B%86%E5%90%88/" title="大数据之多范式编程语言 Scala07_集合"><img src="/oct25-xxxxx/img/cover_img/ctt50.png" onerror="this.onerror=null;this.src='/oct25-xxxxx/img/404.jpg'" alt="大数据之多范式编程语言 Scala07_集合"/></a><div class="content"><a class="title" href="/oct25-xxxxx/2024/02/22/50%E5%A4%A7%E6%95%B0%E6%8D%AE%E4%B9%8B%E5%A4%9A%E8%8C%83%E5%BC%8F%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80Scala07_%E9%9B%86%E5%90%88/" title="大数据之多范式编程语言 Scala07_集合">大数据之多范式编程语言 Scala07_集合</a><time datetime="2024-02-22T08:16:10.000Z" title="发表于 2024-02-22 16:16:10">2024-02-22</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/oct25-xxxxx/2024/02/21/49%E5%A4%A7%E6%95%B0%E6%8D%AE%E4%B9%8B%E5%A4%9A%E8%8C%83%E5%BC%8F%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80Scala06_%E9%9D%A2%E5%90%91%E5%AF%B9%E8%B1%A1/" title="大数据之多范式编程语言 Scala06_面向对象"><img src="/oct25-xxxxx/img/cover_img/ctt49.png" onerror="this.onerror=null;this.src='/oct25-xxxxx/img/404.jpg'" alt="大数据之多范式编程语言 Scala06_面向对象"/></a><div class="content"><a class="title" href="/oct25-xxxxx/2024/02/21/49%E5%A4%A7%E6%95%B0%E6%8D%AE%E4%B9%8B%E5%A4%9A%E8%8C%83%E5%BC%8F%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80Scala06_%E9%9D%A2%E5%90%91%E5%AF%B9%E8%B1%A1/" title="大数据之多范式编程语言 Scala06_面向对象">大数据之多范式编程语言 Scala06_面向对象</a><time datetime="2024-02-21T07:01:10.000Z" title="发表于 2024-02-21 15:01:10">2024-02-21</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/oct25-xxxxx/2024/02/20/48%E5%A4%A7%E6%95%B0%E6%8D%AE%E4%B9%8B%E5%A4%9A%E8%8C%83%E5%BC%8F%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80Scala05_%E5%87%BD%E6%95%B0%E5%BC%8F%E7%BC%96%E7%A8%8B/" title="大数据之多范式编程语言 Scala05_函数式编程"><img src="/oct25-xxxxx/img/cover_img/ctt48.png" onerror="this.onerror=null;this.src='/oct25-xxxxx/img/404.jpg'" alt="大数据之多范式编程语言 Scala05_函数式编程"/></a><div class="content"><a class="title" href="/oct25-xxxxx/2024/02/20/48%E5%A4%A7%E6%95%B0%E6%8D%AE%E4%B9%8B%E5%A4%9A%E8%8C%83%E5%BC%8F%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80Scala05_%E5%87%BD%E6%95%B0%E5%BC%8F%E7%BC%96%E7%A8%8B/" title="大数据之多范式编程语言 Scala05_函数式编程">大数据之多范式编程语言 Scala05_函数式编程</a><time datetime="2024-02-20T12:05:11.000Z" title="发表于 2024-02-20 20:05:11">2024-02-20</time></div></div></div></div></div></div></main><footer id="footer" style="background-image: url('/oct25-xxxxx/img/top_img/top_bg11.jpg')"><div id="footer-wrap"><div class="copyright">&copy;2020 - 2024 By Oct25-X</div><div class="framework-info"><span>框架 </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="translateLink" type="button" title="简繁转换">繁</button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="回到顶部"><i class="fas fa-arrow-up"></i></button></div></div><div id="local-search"><div class="search-dialog"><nav class="search-nav"><span class="search-dialog-title">本地搜索</span><span id="loading-status"></span><button class="search-close-button"><i class="fas fa-times"></i></button></nav><div class="is-center" id="loading-database"><i class="fas fa-spinner fa-pulse"></i><span>  数据库加载中</span></div><div class="search-wrap"><div id="local-search-input"><div class="local-search-box"><input class="local-search-box--input" placeholder="搜索文章" type="text"/></div></div><hr/><div id="local-search-results"></div></div></div><div id="search-mask"></div></div><div id="rightMenu"><div class="rightMenu-group rightMenu-small"><div class="rightMenu-item" id="menu-backward"><i class="fa-solid fa-arrow-left"></i></div><div class="rightMenu-item" id="menu-forward"><i class="fa-solid fa-arrow-right"></i></div><div class="rightMenu-item" id="menu-refresh"><i class="fa-solid fa-arrow-rotate-right"></i></div><div class="rightMenu-item" id="menu-home"><i class="fa-solid fa-house"></i></div></div><div class="rightMenu-group rightMenu-line rightMenuOther"><a class="rightMenu-item menu-link" href="/archives/"><i class="fa-solid fa-archive"></i><span>文章归档</span></a><a class="rightMenu-item menu-link" href="/categories/"><i class="fa-solid fa-folder-open"></i><span>文章分类</span></a><a class="rightMenu-item menu-link" href="/tags/"><i class="fa-solid fa-tags"></i><span>文章标签</span></a></div><div class="rightMenu-group rightMenu-line rightMenuNormal"><a class="rightMenu-item" id="menu-translate"><i class="fa-solid fa-earth-asia"></i><span>繁简切换</span></a><div class="rightMenu-item" id="menu-darkmode"><i class="fa-solid fa-moon"></i><span>切换模式</span></div></div></div><div id="rightmenu-mask"></div><div><script src="/oct25-xxxxx/js/utils.js"></script><script src="/oct25-xxxxx/js/main.js"></script><script src="/oct25-xxxxx/js/tw_cn.js"></script><script src="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.umd.js"></script><script src="/oct25-xxxxx/js/search/local-search.js"></script><div class="js-pjax"></div><script defer src="https://npm.elemecdn.com/jquery@latest/dist/jquery.min.js"></script><script defer data-pjax src="/js/rightMenu.js"></script><script defer="defer" id="ribbon" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/canvas-ribbon.min.js" size="150" alpha="0.6" zIndex="-1" mobile="false" data-click="true"></script><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/activate-power-mode.min.js"></script><script>POWERMODE.colorful = true;
POWERMODE.shake = true;
POWERMODE.mobile = false;
document.body.addEventListener('input', POWERMODE);
</script><script id="click-heart" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/click-heart.min.js" async="async" mobile="false"></script><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div></body></html>